Categories
c++ user-interface

How do I build a graphical user interface in C++? [closed]

331

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?

7

  • 7

    It depends. What sort of operating system are you targeting? Many GUI frameworks are OS-specific.

    Jul 27, 2009 at 1: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, 2009 at 1:03

  • 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 gtk.php.net and PHP-Qt 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, 2009 at 2: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, 2009 at 6:43

  • 7

    “Writing GUIs in c++ is harder than in many other languages” In what are you basing this claim?

    – piotr

    Jul 27, 2009 at 8:08

215

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.

1

  • 4

    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 2, 2019 at 10:08

187

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.

9

  • 26

    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, 2009 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, 2010 at 17:09

  • 22

    @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”?

    Sep 19, 2011 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, 2011 at 0:05

  • 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 ?

    May 5, 2020 at 4:28

81

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.

1

  • 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 8, 2019 at 21:32