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?
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
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.
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.
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.
OS independent algorithm “Creating GUI applications in C++ in three steps“:
Install Qt Creator
Create new project (Qt Widgets Application)
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.