343

All of my C++ programs so far have been using the command line interface and the only other language I have experience with is PHP which doesn't support GUIs.

Where do I start with graphical user interface programming in C++? How do I create one?

Community
  • 1
  • 1
waiwai933
  • 14,133
  • 21
  • 62
  • 86
  • 7
    It depends. What sort of operating system are you targeting? Many GUI frameworks are OS-specific. – Michael Ratanapintha Jul 27 '09 at 01:03
  • 3
    What OS / platform? Or, if you're interested in writing cross-platform GUI apps, please say so - these things make a *huge* difference for C++... – Shog9 Jul 27 '09 at 01:03
  • 1
    It doesn't really matter. But let's say Windows XP as an example. – waiwai933 Jul 27 '09 at 01:05
  • 3
    PHP DOES support writing GUI programs. There's no reason you can't run PHP programs from the command line, and there are gui libraries like PHP-GTK http://gtk.php.net/ and PHP-Qt http://www.php-qt.org/ that can help You also might like to evaluate why you need c++. Writing GUIs in c++ is harder than in many other languages, and you might be able to bundle your c++ code up into libraries callable from Python or C# or PHP or something a little easier to code GUIs for. – kibibu Jul 27 '09 at 02:58
  • 7
    Your question is so broad that I'm thinking you don't just want the names of toolkits or even just some tutorials. You want some *books*, ones that give you more than APIs or examples, that actually give you concepts. KTC recommends some below, but I would suggest, whatever toolkit you pick to start playing with, make sure to find one you can read about too. – quark Jul 27 '09 at 06:43
  • 8
    "Writing GUIs in c++ is harder than in many other languages" In what are you basing this claim? – piotr Jul 27 '09 at 08:08
  • 1
    @waiwai933 So what did you do finally? What did you use for GUI? – praxmon Feb 10 '14 at 11:02

8 Answers8

228

Essentially, an operating system's windowing system exposes some API calls that you can perform to do jobs like create a window, or put a button on the window. Basically, you get a suite of header files and you can call functions in those imported libraries, just like you'd do with stdlib and printf.

Each operating system comes with its own GUI toolkit, suite of header files, and API calls, and their own way of doing things. There are also cross platform toolkits like GTK, Qt, and wxWidgets that help you build programs that work anywhere. They achieve this by having the same API calls on each platform, but a different implementation for those API functions that call down to the native OS API calls.

One thing they'll all have in common, which will be different from a CLI program, is something called an event loop. The basic idea there is somewhat complicated, and difficult to compress, but in essence it means that not a hell of a lot is going in in your main class/main function, except:

  • check the event queue if there's any new events
  • if there is, dispatch those events to appropriate handlers
  • when you're done, yield control back to the operating system (usually with some kind of special "sleep" or "select" or "yield" function call)
  • then the yield function will return when the operating system is done, and you have another go around the loop.

There are plenty of resources about event-based programming. If you have any experience with JavaScript, it's the same basic idea, except that you, the scripter, have no access or control over the event loop itself, or what events there are, your only job is to write and register handlers.

You should keep in mind that GUI programming is incredibly complicated and difficult, in general. If you have the option, it's actually much easier to just integrate an embedded webserver into your program and have an HTML/web based interface. The one exception that I've encountered is Apple's Cocoa + Xcode + interface builder + tutorials that make it easily the most approachable environment for people new to GUI programming that I've seen.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Breton
  • 15,401
  • 3
  • 59
  • 76
  • 5
    "*it's actually much easier to just integrate an embedded webserver into your program and have an html/web based interface.*" It depends. If you want to do fancy stuff (complex UI, custom controls, animations, etc.), yes. If you simply want a typical GUI (with the usual controls and a native feeling), no. – Acorn Jun 02 '19 at 10:08
188

There are plenty of free portable GUI libraries, each with its own strengths and weaknesses:

Especially Qt has nice tutorials and tools which help you getting started. Enjoy!

Note, however, that you should avoid platform specific functionality such as the Win32 API or MFC. That ties you unnecessarily on a specific platform with almost no benefits.

vog
  • 23,517
  • 11
  • 59
  • 75
  • 27
    Personally, I think programming to the Win32 API is beautiful. I likes it much more than using Qt, and that in and of itself is a benefit. Maybe that's just me, though. – mrduclaw Dec 26 '09 at 14:50
  • 7
    My personal experience is exactly the opposite. The Win32 API caused more hassle for me than any other API. – vog Aug 10 '10 at 17:09
  • 23
    @mrduclaw, "I think programming to the Win32 API is beautiful". I TRY at all costs to write portable code. (please note I used the word "try" because there are exceptions...) How is writing non-portable code considered "beautiful"? – Trevor Boyd Smith Sep 19 '11 at 18:17
  • 12
    @Trevor Boyd Smith: Different goals, I suppose. Almost none of the code I have ever written professionally could possibly have been cross-platform so that's never been a concern for me. – mrduclaw Sep 20 '11 at 00:05
  • I think @KTC is right - systemcalls is made through the API to the OS-kernel. The library is distributed from other sources such as GTK. – Björn Hallström May 26 '14 at 13:26
  • Nowadays, the Win32 API is quite cross-platform due to Wine and related projects. Unless an app is using obscure or undocumented behavior, it is likely to run fine in other systems. Then there is virtualization, too. – Acorn Jun 02 '19 at 09:58
  • 1
    million dollar question is that the guys who make game trainers just supply small exe and their programs have beautiful UI so i wonder how they build UI they do not supply these 3rd party libaries with it ? – user889030 May 05 '20 at 04:28
  • 1
    @user889030 There are GUI libraries with a very small footprint, which are usually statically linked into the binary. Note also that game trainers usually either have a comparatively "simple" UI, and/or reuse UI features of the game engine around it. I added "Dear ImGui" to my answer, to provide an example of such a library as well. – vog May 05 '20 at 14:22
  • 1
    Depends on the needs but might be much "easier" to just create frontent that will be opened from browser (lets say react/angular SPA) and in c++ just create backend that does whatever the frontent requests (i.e. create+implement API)... One can get in super impressive UI (way more impressive than QT examples show) – David Constantine Mar 19 '22 at 19:24
82

OS independent algorithm "Creating GUI applications in C++ in three steps":

  1. Install Qt Creator

    enter image description here

  2. Create new project (Qt Widgets Application)

    enter image description here

  3. Build it.

Congratulations, you've got your first GUI in C++.

Now you're ready to read a lot of documentation to create something more complicate than "Hello world" GUI application.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
  • In the past, I'd had trouble with other GUIs not being beginner-friendly enough. But, Qt has some great tutorials. Thank you for the recommendation! – Aces Oct 08 '19 at 21:32
27

Given the comment of "say Windows XP as an example", then your options are:

  • Interact directly with the operating system via its API, which for Microsoft Windows is surprise surprise call Windows API. The definitive reference for the WinAPI is Microsoft's MSDN website. A popular online beginner tutorial for that is theForger's Win32 API Programming Tutorial. The classic book for that is Charles Petzold's Programming Windows, 5th Edition.

  • Use a platform (both in terms of OS and compiler) specific library such as MFC, which wraps the WinAPI into C++ class. The reference for that is again MSDN. A classic book for that is Jeff Prosise's Programming Windows with MFC, 2nd Edition. If you are using say CodeGear C++ Builder, then the option here is VCL.

  • Use a cross platform library such as GTK+ (C++ wrapper: gtkmm), Qt, wxWidgets, or FLTK that wrap the specific OS's API. The advantages with these are that in general, your program could been compiled for different OS without having to change the source codes. As have already been mentioned, they each have its own strengths and weaknesses. One consideration when selecting which one to use is its license. For the examples given, GTK+ & gtkmm is license under LGPL, Qt is under various licenses including proprietary option, wxWidgets is under its own wxWindows Licence (with a rename to wxWidgets Licence), and FLTK is under LGPL with exception. For reference, tutorial, and or books, refer to each one's website for details.

KTC
  • 8,967
  • 5
  • 33
  • 38
19

Since I've already been where you are right now, I think I can "answer" you.

The fact is there is no easy way to make a GUI. GUI's are highly dependent on platform and OS specific code, that's why you should start reading your target platform/OS documentation on window management APIs. The good thing is: there are plenty of libraries that address these limitations and abstract architecture differences into a single multi-platform API. Those suggested before, GTK and Qt, are some of these libraries.

But even these are a little too complicated, since lots of new concepts, data types, namespaces and classes are introduced, all at once. For this reason, they use to come bundled with some GUI WYSIWYG editor. They pretty much make programming software with GUIs possible.

To sum it up, there are also non free "environments" for GUI development such as Visual Studio from Microsoft. For those with Delphi experience backgrounds, Visual Studio may be more familiar. There are also free alternatives to the full Visual Studio environment supplied from Microsoft: Visual Studio Express, which is more than enough for starting on GUI development.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Spidey
  • 2,508
  • 2
  • 28
  • 38
18

I found a website with a "simple" tutorial: http://www.winprog.org/tutorial/start.html

ZippyV
  • 12,540
  • 3
  • 37
  • 52
  • 14
    This introduces the Win32 API, a very low level approach to GUI programming that ties you to the Windows platform. I would not recommend that. – vog Jul 27 '09 at 01:18
  • 20
    It's a valid option. I agree that it might not be the best place to start, but it doesn't deserve a down vote. – Emil H Jul 27 '09 at 01:21
  • 2
    I know this is not the ideal solution but it does help to learn how the gui system in Windows works. I've read this tutorial a couple of years ago and now I know what the message loop is and how it works. When working with winforms in .net this knowledge might come in handy. – ZippyV Jul 27 '09 at 01:45
  • 7
    If you are going to write desktop applications there is no substitute for knowing how the OS really renders and interacts with the user. If you just use a library without understanding the fundamentals then you'll never really understand what is going on. Learning a little bit of low level win32 gui programming will be time well spent. – Jim In Texas Jul 27 '09 at 01:53
  • @Jim In Texas: I agree, but this "bit of low level" should be learned _after_ the basics, so I still find that recommendation inappropriate for beginners. – vog Jul 27 '09 at 02:25
  • Certainly worth a newbie looking at the get the hang of how the native GUI actually works – paulm Mar 26 '15 at 21:16
4

I use FLTK because Qt is not free. I don't choose wxWidgets, because my first test with a simple Hello, World! program produced an executable of 24 MB, FLTK 0.8 MB...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pernecker
  • 49
  • 3
3

It's easy to create a .NET Windows GUI in C++.

See the following tutorial from MSDN. You can download everything you need (Visual C++ Express) for free.

Of course you tie yourself to .NET, but if you're just playing around or only need a Windows application you'll be fine (most people still have Windows...for now).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Justicle
  • 14,761
  • 17
  • 70
  • 94