17

I've been learning C++ for about a month now, and before I go any further, I'd like to clear up this tedious question I keep on having. I know what a GUI is, but I don't really know how it works, and maybe examples of popular ones?

Although I know command line programming is the bare fundamentals, I think it'd be fun messing around with a GUI.

Although I have around 3 million other questions, I'll save them :D

Rachel
  • 24,037
David
  • 461

5 Answers5

29

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.

8

Now is as good a time as ever to learn GUI porgramming. As you know C++, I would recommend looking at QT. Great documentation, huge user base and lots of examples/tutorials avalible to learn from.

mattnz
  • 21,490
3

You may also start with Microsoft Visual Studio C++ Express. The IDE is very friendly and easy to use (and free!), and you will be able to to create your first GUI applications very quickly... Which will help you concentrate on understanding the basics of event-driven programming, a different approach that you will need to master.

You may also directly start with building WPF driven applications, but I would start with Windows Forms at first, that is one technology less to grasp since it's just basic C++ with Microsoft's Windows Forms API.

Jalayn
  • 9,827
0

Maybe more suited to StackOverflow, tagged c++; but anyway.

Look a few of these up on google; Qt, WxWidgets, TheForgers' WinApi, Fltk...

GUIs aren't that hard to use, especially once you already know c++. Go with WxWidgets I reckon; Qt is a bit too complex for a month's learning. You can pick up Wx in a couple of hours.

-1

Users don't like complicated things. Console is a complicated thing, that's why you have to create GUI applications which are more friendly and easy to understand. This is the most obvious reason I see. You can look at gtk or Qt - those two are the most popular ones.

Sergey
  • 2,713