I am generalizing over a couple of GUI libraries but on a very high level the most important concept that you need to understand is that a GUI is event driven.
In a console application your user input usually happens at certain points that you defined. You prompt your user, you wait for his input, you calculate something based on that input. One of the main differences is that the input happens only in one place, you are reading text from the commandline (stdin
in C++).
In a GUI application you typically have multiple places where input could happen, for example buttons that perform different actions or text fields. This is where events come into play. For example, clicking a button triggers an event. This event needs to be handled by an event handler, which is usually just a method that takes an event object an is called when such an even is triggered. How does your button know about the event handler? You usually subscribe (or listen to it).
Here is a "C++ inspired" example, this is not actual QT or C++ code.
class MyClickHandler : public ClickListener{
void clickHandler(ClickEvent e){
showAlertBox("The button "+e.getSource().getName()+" has been clicked");
}
};
When you create the Button, you register an instance of the MyClickHandler class against the button.
...
MyClickHandler handler();
Button b("Save");
b.registerListener(handler);
...
Now every time the Button b
gets clicked a message box shows up saying "The button Save has been clicked".
You can imagine a GUI application has two stages:
- Establish the GUI: A short period at the startup, where all the objects are created and connected with each other.
- The event loop: Your GUI is in one big while loop and is just sitting there idling until an event is triggered.
This is a very simple example, but I would recommend with whatever framework you pick up you try showing a message box when a button is clicked.
For the framework, there are a lot of them out there: In the case of C++ I would probably recommend Qt.
One last word of advice: Stay away from GUI designers until you really know what is happening in the background. It is not that hard to write some simple examples and to understand the event loop first and then move on to more complex layouts.