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
  • 23,979
  • 16
  • 91
  • 159
David
  • 461
  • 2
  • 6
  • 11

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.

sebastiangeiger
  • 1,907
  • 13
  • 15
  • If you add a semicolon to the end of the class definition, and make the inheritance public, it would be proper/possible C++ syntax. – Lstor Aug 02 '11 at 10:29
  • 1
    Done. But I did not want to raise any false expectations, since the code won't be runnable after all. Most of my experience is in Swing which probably reflects in the class names. The same concepts will be called differently in different frameworks. – sebastiangeiger Aug 02 '11 at 10:32
  • 1
    +1 This seems the only answer that answers the question (to me it seems he's not asking only about frameworks, he's asking about general concepts) – Federico klez Culloca Aug 02 '11 at 10:42
  • 1
    Ewwww.... ugly listener crap. – DeadMG May 11 '12 at 16:22
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,315
  • 5
  • 54
  • 83
  • 1
    +1 for Qt - @David - Qt has it's own development environment called Qt Creator which is self contained, free and relatively simple compared to others. Qt is powerful, intuitive, extensive, very well documented and with large and active user base. It is easy to get started and there are lots of examples to start playing with and getting to do your own thing - which is a great way to learn by the way. It can be downloaded [here](http://qt.nokia.com/products/developer-tools) – Roger Attrill Aug 02 '11 at 08:38
  • I’m inclined to downvote this. QT encourages a lot of really bad practices in C++ if you don’t know what to look out for. – Which you don’t, after just one month of C++. Furthermore, it touches on some complex aspects in C++. If possible, I would start GUI programming in a different language first, and concentrate on getting C++ right. – Konrad Rudolph Aug 02 '11 at 12:39
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,789
  • 4
  • 39
  • 58
  • 1
    In VC++ Express you can write GUI with: 1. Win32 API which is very old, cumbersome and just ridiculous 2. MFC which is old pile of crap 3. WTL which is better but not cross-platform. 4. C++/CLI which is unpopular, badly supported (even no intellisense) and too complicated. Qt is easier, cross-platform, has nice GUI designer and has large amount of useful non-GUI classes which is very important in C++ with its small standard library. – Anton Barkovsky Aug 02 '11 at 10:31
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,693
  • 3
  • 18
  • 22
  • 2
    CLI is not complicated and GUI's by definition are MORE complicated for the user. They are more accessible not less complicated. – Ominus Aug 02 '11 at 17:43