How to connect C++ code to an application? - c++

Suppose I have some function, say in C++
int f() {
// do some stuff
}
How do I connect it to a button in some application? It does not need to be a web application per se, it can be a normal desktop application (not a big difference, I guess).
What all do I need to do to get this function to be called when I press a button in some application?
I guess the question is too vague to be answerable as such.
I am reading the Head First Design Patterns book, and it has a lot of code samples for some dummy apps. Of course all that in is Java. But I am more familiar with C++. So, while reading, I was just wondering how I could possibly get my code connected to some app. What is the general procedure?
For concreteness, how do I connect it to a button on a webpage?

Yes, there's actually quite a large difference between a desktop application and a web application in this respect -- at least usually, though there are exceptions (e.g., Microsoft's Silverlight framework lets you do some things the same in a web app as you can in a desktop app).
How you connect things up on a desktop app varies widely. Raw Windows applications frequently include a giant switch statement based on the value of a "message". MFC uses what it calls Message Maps. Qt uses what it calls signals and slots.
It's probably worth mentioning, however, that in a typical case you don't really deal much with this in code. Most people let an IDE handle most of the details of connecting this up. It'll (typically) produce a skeleton function about like you have above (possibly with even more filled in to do things like calling parent class functions, etc.) so all you normally have to deal with directly is the //do some stuff part.
Edit (in response to edit of question): a web page is probably the last thing you want to deal with right now. There's simply a huge range of variation in how you handle button events and such on web pages. In particular, you frequently get a combination of actions that happen inside the browser (e.g., in Javascript) and things that happen on the server (e.g., using AJAX). You can invoke functions on either the client, the server, or both, in response to a particular button click, etc., so this is actually quite a complex scenario in general.
While there's certainly variation between windowing systems and frameworks in desktop applications, at least the variation is over a rather smaller range.

Basically, what you're noticing is that C++ doesn't come with a standard UI. Java does. (It in fact has a number of standard UI's such as Swing, AWT, SWT). This makes it hard to say how you'd connect a button.
Now, the problem is a lot easier if you choose a UI library to use in combination with C++. Qt is a popular choice, because it is easy to use, portable and effective.
With Qt, the answer has two parts: You declare int f() in the public slots: part of your dialog class, and in your dialog's constructor you connect the button's clicked signal to the int f() slot.

Related

controlling an application via c++ code

I am writing an application in C++ in windows, that has a UI (WxWidgets) and user normally use the application via its UI.
Now I have a new requirement, the application needs to start and controlled by another application.
I can not develop a DLL or similar solutions.
I have access to my code (apparently!) and the other applications is developed by other users, but I can give them details on how to control my application.
My question is: How can I allow other applications to control my application via a defined interface?
For simplicity assume that I developed a calculator (has UI) and I want to give other application to do math on my application (for example they may ask my application to add two numbers and so on, As the math is very time consuming, I need to inform them about progress and any error that generate during processing.
Can I open a pipe to communicate?
Any other way to achieve this?
You can use pipes or tcp/sockets with a custom protocol, but probably it's better if you split your application in two parts:
One part that does the computation
The user interface
and publish the first one as an http server responding to JSON requests.
Using a standard protocol can ease up testing and increases interoperability (you can also probably leverage already existing libraries for both implementing the server and the JSON marshalling).
Note that in addition to accepting commands, any error message you are going to show for example in a message box or any other nested event loop like dialog boxes need to be rewired properly; this can be very problematic if message or dialog box come up as the result of calls to external code that you didn't write yourself.
This is the typical change that would have costed 10 if done early and that will cost 1000 now.

How to shell to another app and have it appear in a Borland VCL form (c++)

I'm actually in charge of a FIP networking c++ application, working for the first time with Embarcadero C++ Builder XE5.
The app is nearly finished, but I can't find how to implement the last feature...
I wanted to open an external Windows HyperTerminal in order to see what happen on a given COM port, for this purpose I'm using ShellExecute() to launch it from my application but it's a bit ugly since there is 2 different windows.
I was wondering if it was possible to integrate this newly opened HyperTerminal into an existing form (Panel for instance). I couldn't find nothing related excepted this =>
Delphi style, but i don't understand a byte of #mghie answer since it's delphi.
If anyone have a clue I'm really interested, even the most basic clue!
For almost all of my projects where COM port interaction is needed I use AsyncPro. The project is very well documented with a ~1000 page reference manual.
Reference Manual
Developer's Guide
For this case, the package provides a VCL terminal that simply drops onto a form. It's quite flexible with a lot of options to configure its behaviour.
I wanted something similar in past but with no success.
1.The only thing I was able to do is the exact opposite.
dock my VCL window inside another (not VCL app) but that solved my problems
If you terminal is console window then I doubt even this can be done.
anyway find handle of desired window
find handle to a dockable subcomponent
set the parent of your subwindow to it / or use manual dock
2.maybe you can do some funny stuff
like hide terminal somewhere
and continuoslly copy its graphics to your window
newer done that hide thing
but copy the contents is doable (although on windows a little unstable sometimes)
done it once to feed my App with IR-camera feed from different App
while 'focus' stays on hidden terminal it should work
also you can try to post messages to it somehow if you need the focus ...
Sorry for a vague answer but at least you see some approaches of mine
maybe someone has a better way to do this

How to add custom class processing functionality in Qt

I'm a little new to Qt. I have been programming in C++ for quite a while.
I want to create an application in Qt/C++ just because it's very easy to create the GUIs. I wanted to know that is it possible that I can write a C++ class in native C++ and use it in my Qt application.
I want to write the GUI in Qt and do all processing using my native C++ code rather than Qt/C++. Is it possible to compile a DLL in native C++ and then load and use it in QtC++ application?
I really want to write a GUI application using C++.
There is no such thing as "native C++" as opposed to "Qt/C++", so the question makes little sense. All of core Qt code is compiled native C++, just as any C++ application that uses Qt is.
As JBentley has duly noted:
[You may be] confused because of the code generating tools Qt uses, like moc [or uic]. Those don't mean that C++ compiles natively while Qt doesn't. All those tools do is provide a convenient way to produce a lot of boiler code that the framework relies on, so that the programmer can pretend they don't exist and use things like Qt's slots and signals. The final code which you compile and link is ordinary C++, with calls into the Qt library (also C++) which you've used.
If what you're asking is "am I forced to use Qt classes in all parts of my application", then no - nobody's forcing you to do that. If you already have parts of your application written using boost, or standard C++ library classes, it's fine.
You have to understand that Qt has modular design and provides a lot of non-GUI functionality. You can use the non-gui modules in the non-gui part of your code. It's a fairly clean and general purpose application development framework. It's perfectly fine to use it for various internet server applications, for example.
The following Qt modules are of note for non-gui development:
Core: Core non-graphical classes: containers, event loop, timers, threads, state machines, internationalization, XML, json, file I/O - all of it portable.
Network: Classes to make network programming easier and more portable. Includes secure sockets, HTTP requests, etc.
SQL: Classes for database integration using SQL.
You are able to use any C++ code you would like. I have have actually had questions with the same thought process in my day to day work. First you need to grok the idea that Qt uses normal C++ and special tools to create a robust set of libraries.
Later on come back and you will want to learn more about the items below:
Signals and Slots
This is how events are wired together.
One objects sends a signal and all connected slots get called with the given parameters.
Signals and slots are an example of the Observer Pattern.
Designer Forms
The form designer lets you lay out controls using simple drag and drop operations.
The .ui file gets transformed into C++ code as part of the build process.
Layouts
You can do a lot of things with just the horizontal, vertical and grid layouts
Spacers
You can push things up, down, left, or right using different types of spacers
They look like a spring on the form designer.
Resource Files
Lets you embed graphics and other content directly into the application.
Models, Views and Delegates
Hard to avoid if your have a non trivial UI
There are other things too, but knowledge in these areas will let you make some decent desktop applications. My favorite reference is older, but still a goodie: "C++ GUI Programming with Qt4" http://www.amazon.com/Programming-Edition-Prentice-Software-Development/dp/0132354160
Yes QT4 is "old" and QT5 has been out for some time... but the book provides a solid foundation that you will be able to add information to.

Best Practise for GUI development for vanilla C++ application

I want to know about developing of gui to vanilla C++ application. I have experience in mostly in command line application. My experience in C++ gui till now is cout and cin. I have some experience in WPF (just to mention that I do have some gui experience) I hope this will describe my level of experience with gui. Now,I want to develop an application which needs separate GUI with possibility of 3D display. Of course, one of the choice for GUI API is Qt.Also,after reading lots of stuff on internet, i think code separation would be really helpful in future.
So, here goes my question: Is it possible if I keep my logic as generic as possible (not using winapi or qt in my logic) and make only GUI part API specific (say winapi or qt). Or I will need to add some code in my logic section, say for synchronization between logic and GUI. One can say signal and slot from qt is possible but as far as i know signals and slots are qt specific. they are not standard C++.
In summery,my question is can i make my logic in standard C++ (to stay platform/ framework independent) and only design platform specific GUI? If yes, could you please suggest a link or tutorial or book. A sample code or implementation would be 'a wish come true'. Also, some insights in code separation tactics would be quite helpful.
Regards!!
EDIT::
I will elaborate my problem. I have an application which has separate GUI and Logic section. The gui uses WinAPI and as the communication mode used is windows messages, previous user has created a HWND object in Logic which communicates to GUI. So, there is a HWND in Logic and HWND in GUI. I don't find this approach satisfying. One of the reason is that Logic part will be edited by non-programmers in later stage (not much. just modification of constants or changing implementation of function without changing para or return value). So, I just want to keep logic part in standard format (as much as possible). So once again, could anyone help me in designing business logic in C++ and GUI in any API.
After reading some threads, I found that answering own question is not a bad practice. So I will share the answer I have got.
There is no standard way to build a C++ gui application without using external dependencies. The synchronization between gui and logic part is always framework specific. So, if I want to develop a C++ gui application, I cannot put logic part in standard C++. It must have some code from external framework which will communicate the logic part and gui part. Having said that, I have found my way in by following method. I am going to put my logic part inside a static lib and then I will attach this lib to gui part. It will increase coding in gui section, but it will keep the base functions in standard c++. This way (a function lib in standard C++ and machine operation in framework specific code) will work for me. I hope I am on right track. :)
You can to a large extent write completely separate logic and GUI code in Qt. However you will be able to create a much more useful GUI if you allow the logic and GUI code to interact. Qt has it's own classes for a lot of things (QString, QVector<> etc.) but you're free to ignore these for the most part and use the standard library instead if you'd prefer.
However, I do not think that it is worth trying to separate Qt from the logic code entirely because, as mentioned before, you will be able to build a much better GUI if they interact. For a simple example you could write a very simple Qt GUI with just a window and a button; press that button and some logic code is run. However with more interaction you could use signals and slots to update a progress bar on the GUI to let the user know how far the logic code had got. Also Qt is very portable, allowing you to build your program for Windows, Mac and at least some Linux distros.
Also for your 3D display requirement I recently found myself trying to do a similar thing and found this example very useful - http://qt-project.org/doc/qt-5.0/qtgui/openglwindow.html.
I think that by default, you should aim to separate business logic from presentation (GUI) code. In web development, the most commonly used pattern is MVC, and it's principles apply equally to native applications.
However, this separation might be more difficult to achieve in native applications. Mainly because there are no frameworks such as Symfony, which have already solved these architectural problems, and make it easy to keep UI and business code separate by following the established conventions. I have't used Qt but from what I know it's mainly a GUI toolkit - models, views and controllers are not as well-defined and integrated.
Depending on the nature of the needed interface, a simple OpenGL GUI might suffice. This is what I did for a simple uni project. The project needed to display a shapefile colored according to statistics in an XML file. I created a number of GUI widgets - button, label etc, and a custom map widget which encapsulates "business logic". Perhaps I should have "cleansed" the map widget, making it as generic as possible, and move all business logic to a separate library, but considering the business layer was very thin I thought the added complexity would outweigh the benefits.
Another factor is your skill-set - current, and areas which you want to improve. I was more interested in OpenGL and freeglut, then in learning Qt. If I had known Qt, I would have used that.
I have done some projects with vanilla C++ and a QT GUI. The idea of not mixing any QT code into the pure C++ stuff is always good but takes a lot of fun out of it. Usually you end up with a much more elaborate GUI than you expected and would often like to connect it better/easier to your code. Signals and Slots are a really great way to let the GUI interact with your code, but then you start mixing...
TLDR: Think really hard about why you don't want to mix your code with e.g. QT. It would not meand mixing GUI and Logic, no way, but your life could be a lot easier using QT classes like QStrings, QProcesses or QThreads...
If you manage to do all the interactions using QTs Signal & Slot mechanism, at some point you might even change you native QT GUI to a QT Quick (QML) one, which is highly customizable, has nice looks and animations and whatnot.
This is just my opinion.

C++ Custom GUI Button Question

I am designing a graphical application for which I've decided to write my own menu. I would like this menu to be platform independent. For the time being, my menu will mostly consist of a number of buttons. My issue involves the handling of events when a button is clicked. My dilemma is with a button "knowing" about the context in which it exists. It seems to me that if there is some larger piece of code that creates buttons and handles mouse events, the need for some type of switch statement might arise. The switch statement would have to invoke the appropriate action based on whatever uniquely defined the button that was clicked.
I would like to avoid this switch statement. My first idea was to have each button maintain a function pointer that it uses to blindly initiate the correct action when it is clicked. This would eliminate any button-specific code. Yet, it bugs me that a button should contain any context-specific information (such as a function pointer). I am fairly inexperienced and I am wondering if this is considered bad design. Regardless, how can I design my menu in a manner which eliminates the need for some type of switch statement and is considered good OOP design? I would like to hear what your preferred solutions are.
Thanks in advance!
You take a look at libraries :
SPTK (Simply Powerful Toolkit) is a cross-platform toolkit that provides a set of C++ classes for fast and easy application development. It provides GUI components that use FLTK, and features database support with seamless connection to GUI components.
eGUI
http://www.codeplex.com/egui
http://torjo.com/egui/
http://msdn.microsoft.com/en-us/magazine/cc534994.aspx
NovaTK is an object-oriented, cross-platform GUI toolkit. One of the focuses of NovaTK is to facilitate rapid development of cross-platform applications requiring fewer lines of code. The event system is based upon a powerful callback mechanism that makes application design simpler, easier to read, and logical.
Ecere Cross Platform GUI Applications
Develop applications once, deploy them on all platforms alongside a lightweight runtime environment.
Introduction to wxWidgets
ClassLib, A C++ class library
libgm - A mini GUI library for Windows
You can take a look at the
Command Pattern.
You can associate a command to a menu item, the command would contain the code to be executed.