How to Build a Graphical User Interface in C++

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

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.

Building a graphical user interface for an existing large C++ code

Of your 3 options, I would definitely go with Qt.

Basically for Qt to be a functional GUI, you need to start QApplication, show() some sort of widget and then start the event loop for the application (QApplication::exec()).

http://qt-project.org/doc/qt-4.8/qapplication.html

http://qt-project.org/doc/qt-4.8/qapplication.html#exec

Qt is extremely flexible and well thought out, and has a strong following. And it has incredible documentation.

To interact with your existing C++ data structures, just construct them as a member variable as one of the main widgets that you have for your GUI. Then when you want to access and display information on it, it is a piece of cake.

http://qt-project.org/doc/qt-4.8/qwidget.html

http://qt-project.org/doc/qt-4.8/qmainwindow.html

Most of the GUI elements in Qt only act as the view, and there isn't any definitive Model and Controller setup. That is left to the developer. If you are displaying a database or a tree or a grid of items there is a model/view flow, but I don't think it applies to your application.

Understanding and using SIGNALS and SLOTS is essential to making an interactive GUI in Qt, and is very painless.

http://qt-project.org/doc/qt-4.8/signalsandslots.html

http://qt-project.org/doc/qt-4.8/qobject.html#details

Reading up on all the different kinds of QWidgets out there, you should be able to find each of the elements you listed in your question.

Here are some you should look at:

http://qt-project.org/doc/qt-4.8/qtextstream.html

http://qt-project.org/doc/qt-4.8/qtextedit.html

http://qt-project.org/doc/qt-4.8/qlineedit.html

http://qt-project.org/doc/qt-4.8/qlabel.html

And of course look through the tutorials and examples that come with Qt.

How to use GNUPlot with Qt

http://lists.trolltech.com/qt-interest/2002-12/thread00068-0.html

Also, as a developer that has used both Qt Creator and Eclipse, I prefer Qt Creator, and porting a project to work in Qt Creator is very straight forward. If you want to change the build chain of Eclipse to use the Qt libraries and QMake, it is possible too.

http://qt-project.org/doc/qt-4.8/qmake-project-files.html

http://qt-project.org/doc/qt-4.8/qmake-project-files.html#declaring-other-libraries

http://therning.org/magnus/archives/1023

I hope that is helpful. Good luck.

How to create a classic GUI with C++

GUI libraries are generally called "widget toolkits", and there are a variety of cross-platform GUI widget toolkits available, however they are rarely a good idea for a great UX because different operating-system environments have different UI paradigms (e.g. how macOS and Ubuntu has a single always-on menu-bar at the top, whereas Windows and other Linux desktops do not).

So you have two fundamental choices to make:

  1. Use a single cross-platform GUI toolkit - which means creating a single GUI project, but restricting you to that toolkit's selection of controls and functionality, and generally subjecting your users to a (relatively) poorer user-experience, either because your toolkit does not use native widgets (e.g. Java, WPF, Qt) or because you cannot use a platform's native paradigms.
  2. Write a new native GUI for each platform. This means more work, but a better user-experience.

For cross-platform GUI toolkits, the main options are:

  • Qt
  • GTK+
  • WxWidgets

For a comparison between the three, see this QA: Which, if any, achieves Windows native look: GTK+, wxWidgets, Qt, FLTK?

(TL;DR: WxWidgets uses native controls but is difficult to develop with; Qt offers the best developer experience but has many other bullet-points which you need to be aware of)

For a native UI for each platform you need to target, you'll need to familiarize yourself with each API:

  • Win32:

    • MFC (the API is ugly by modern standards, but does encapsulate the main Win32 UI paradigms and APIs, such as Common Controls. However, be prepared for an uphill fight to support latter-day UI features, such as High-DPI, bi-directional text and high-quality 2D rendering. GDI/GDI+ is on the way out, which means using Direct2D - which is fun because MFC assumes GDI)
    • UWP/XAML: If your application can be sandboxed (and run only on Windows 10) then you should take a good look at UWP/XAML (it's all native, so the CLR is not involved).
    • WPF: Using WPF makes it easier to create a high-quality user-interface, if you're comfortable with your application taking a dependency on the .NET Framework and writing UI code in C# - however the visual aesthetic of un-skinned WPF apps took a nosedive with Windows 10 (i.e. they're ugly) and WPF's default control set is very aneamic - but if you have the means (i.e. time, money, people) then you can get great results - and as a bonus the XAML for WPF is generally portable to UWP.
  • macOS: Cocoa - you will have a hard time doing this in C++. If you're targeting macOS I strongly suggest writing your UI layer in Swift (or Objective-C if you have to) and then linking-in to the rest of your application code using C++ bridges.
  • Linux: As Linux is just a kernel the set of "native" widgets depends on the desktop environment of the user - but most desktop programs seem to use GTK. And if you're going to use GTK then you might as well use GTK's cross-platform features to support Windows and macOS.

In conclusion: it's difficult and a lot of work. :) - and explains why many software titles today (mid-2017) often built as web-applications either on the web directly (e.g. Facebook, StackOverflow, SalesForce) or hosted webviews using Electron (e.g. Slack, VS Code, Atom) or some other hosted-webiew (PhoneGap apps, Spotify, etc).

How to create gui in C++

Personally I'm a fan of Qt.

However, it depends entirely on what you want to do. Qt is primarily for cross platform development, so it'll look and act mostly the same between any platform, it also has a large library that may require a bit of a learning curve at first - but the licencing options make it look pretty. Also the documentation is very awesome.

There are of course a lot of other options like:
GTKmm (based on GTK+), wxWidgets, FLTK, etc...

Also this is a duplicate question, so look at some of these other answers:

How do I create a GUI for a windows application using C++?

How do I build a GUI in C++?

GUI for C++ newbie

Some have mentioned Qt, some wxWidgets.

A quick rundown on crossplatform gui toolkits that you could try:

  1. Qt: Looks appropriately native on whichever platform you use it on. It has its own build system however, which doesn't always like to play nicely with others. If you want the "beginners" experience in making a GUI with Qt, I recommend Qt Creator. It's a standalone IDE built by the developers of Qt.
  2. wxWidgets: While Qt looks native (it tries its best to emulate the look and feel of the OS you are running on). wxWidgets is native. That is, it uses the GUI elements provided by the operating system. Last I looked at wxWidgets the C++ library was falling behind as far as modern development practices go. You are possibly more likely to learn bad programming habits from this library.
  3. gtkmm: This is from the same guys who make the GIMP and Gnome, which as a MacOS user probably doesn't mean a lot to you. They don't try nearly as hard to fit in as wxWidgets and Qt. However, they probably have the most modern C++ library. They have done a lot of work to use modern C++ development practices. This can be helpful for a new programmer, as you are less likely to learn bad habits from them. On the downside, you'll get thrust into the land of templates and function pointers and such.

Those are the big ones with the most momentum behind them. There are countless others to also consider.

Easiest way to add a GUI to C++ app

The statements about ISO C++ in this answer's comments are poorly edited. None of the solutions presented here would impose on the computational code a requirement of changing to a different dialect of C++. The GUI code itself might be another story.

The answers about using Windows Forms with managed C++ are probably the most practical. A lot of the UI-related code would be in a dialect (extension) of C++ where the .NET-garbage-collected pointers would be living alongside the traditional ISO C++ pointers. I haven't used this method but from what I read it might be better than what I have used.

MFC might be more practical if you don't want to invest in .NET knowledge for this task. It uses standard C++ constructs to wrap the Windows API. The Windows API uses a particular calling convention which C++ doesn't normally use, and it takes an extension to C++ to work with that, but this is no different than having a C++ program that calls some functions that are extern "C". Visual Studio has a GUI layout tool that is really good at layout of dialogs and at interfacing the dialog widgets to variables that reflect the state of the widgets. I've used this. If you can boil your GUI down to a dialog box then this would be a great option, otherwise you'd be using one of MFC's layout-managed windows.

Obviously the above options do not handle other platforms you might have in mind.

There are also various toolkits that were born on other platforms and have been decently ported to Windows. GTK, QT, FLTK, and WxWindows come to mind. I haven't used any of the dialog-layout/application designer tools that work with those, and I haven't used QT at all. When I last reviewed QT it had a special preprocessing pass for the GUI code. Other than that these portable tool kits are pure ISO C++ as far as I know.

As a really-out-there option one could program to the X Window System protocol using "libx". As far as I know this would involve no non-ISO C++ code.

Creating GUIs in C++

The C++ language by itself has no standard way to create GUIs. To do that, you need to choose a GUI toolkit. Some, like Qt or WxWidgets, are cross plaform, while other, such as MFC are tied to a particular OS.

Most GUI toolkits allow both options, either design the GUI from scratch, or use a graphical GUI builder and implement only the event handlers. It's up to you to choose the method you feel more comfortable with. Personally, I'm used to the Qt toolkit and the Qt4 designer interface designer.

Graphical user interface Tutorial in C

The two most usual choices are GTK+, which has documentation links here, and is mostly used with C; or Qt which has documentation here and is more used with C++.

I posted these two as you do not specify an operating system and these two are pretty cross-platform.

Best Practise for GUI development for vanilla C++ application

After reading some threads, I found that answering own question is not a bad practice. So I will share the answer I have got.

There is no standard way to build a C++ gui application without using external dependencies. The synchronization between gui and logic part is always framework specific. So, if I want to develop a C++ gui application, I cannot put logic part in standard C++. It must have some code from external framework which will communicate the logic part and gui part. Having said that, I have found my way in by following method. I am going to put my logic part inside a static lib and then I will attach this lib to gui part. It will increase coding in gui section, but it will keep the base functions in standard c++. This way (a function lib in standard C++ and machine operation in framework specific code) will work for me. I hope I am on right track. :)



Related Topics



Leave a reply



Submit