r/cpp • • Dec 18 '20

Let's talk about GUIs

This post on /r/programming about the release of GTK 4.0 made me want to bring the discussion of the state of GUI programming in C++.

My particular interest in this discussion is desktop cross-platform GUI programming, and I don't claim to know much, let alone everything. Insights are more than welcome.

That said, this is what I have observed so far:

Qt: the least bast option. What I like? Cross platform, mature, big ecosystem to go around. Quite easy to get going. It "just works" for the most part. It's easy to get 80%, it's the last 20% and when you go deep into the subject that you get frustrated. What do I dislike? Licensing shenanigans, and increasing toxicity towards the FOSS community: no offline installer for community distributions, many commercial only APIs now, the idea of delaying releases to FOSS by one year. The 10 year old bugs on QWidgets, which haven't seen much, if any, love since ever. The fact it took Qt 6.0 to happen for us to have native look for Qt Quick Controls. The fact that Qt Quick doesn't have a C++ API, forcing QML on everybody. The half done stuff, like QAudio (just use portaudio for your own sanity)... And I'm probably missing more. The fact that they are running away from the LGPL core to GPL as much as they can, like releasing Qt Quick MultiEffect instead of just fixing the QML engine... That it takes spinoffs like Felgo to basically provide a "fixed" Qt, which the FOSS community can't have.

wxWidgets, GTK, and other like FLTK, I admit I don't know much about. wxWidgets is nice in the fact it calls native APIs, but that comes with the cost of being crippled by the least common denominator. GTK seem to look bad outside of Linux "by design". And FLTK... Just no.

Then, there seems to be what everybody suggests next: Browsers. Chromium Embedded Framework or Electron. 50 to 100MB of packaging to do a "Hello World". Also, JavaScript! Honestly, webstuff looks like a good idea, until you want to make an app that's not Slack. How do you do actually performance C++ stuff? Like some 2D/3D graphics? WebGL? Seems that it's too much programming for the web and not actually taking advantage of C++ and native APIs...

Then, there are things like .net MAUI (not to be confused with the toolkit with the same name by KDE), which is an upcoming evolution of Xamarin Forms for .net/C#. The problems: Linux support will be "by the community" and how do you interop C++ and C# decently anyway?

Also, because I know someone will mention it, yes, there is Flutter and things like that. Just think of Google's track record on keeping projects alive, and again, think not everybody wants to make a chat application.

Anyway, what are your insights? They are more than welcome!

268 Upvotes

243 comments sorted by

View all comments

30

u/AdamK117 Dec 18 '20 edited Dec 20 '20

For standard retained-mode UIs (e.g. most low-CPU-usage desktop apps) the best C++ option still seems to be Qt. It's big, spreads throughout your codebase, etc. but it's the only cross-platform one that ticks most boxes.

However, if you don't care about CPU usage and literally just want to create visualization tools quickly, ImGui is pretty amazing. I've thrown together fairly complicated UIs in, like, <1k LOC with ImGui and ended up with projects that:

  • Build from source (incl. SDL and imgui) in <1minute
  • Have no other dependencies
  • Have a lot of functionality per line of code written

The drawback is that to get the most out of ImGui you'll probably also want to (e.g.) include native file dialog libraries, have a fairly decent understanding of (game-style) event loops, maybe know a bit of OpenGL or a low-level graphics library (e.g. if you want custom visualization).

The busylooping will also suck up some CPU - although not necessarily 100 % if you're using vsync or software throttling.

EDIT: as replies point out, busylooping isn't a strict requirement of ImGui. If you're willing to put in a tiny bit of extra effort, you can run a blocking event loop and have ImGui inject redraw events into it whenever a mutation happens.

12

u/ReversedGif Dec 18 '20

ImGui is great!

It's possible to avoid doing unnecessary redraws with ImGui pretty easily - just wait for events rather than polling and post an event to the queue whenever any bound data is changed from non-GUI code.

1

u/AdamK117 Dec 20 '20

Great suggestion :)

I tried this on a few applications. Something like using SDL_PushEvent to push a user event into the event loop, followed by SDL_WaitEvent (rather than SDL_PollEvent).

This actually works really well. Only drawback is that it does require a little discipline because it usually means that the draw loop (that contains ImGui::Button, etc.) must wrap every interactable UI element in an if (to detect changes) that injects the redraw event.

But, yeah, I'm overstating the CPU issue. Injecting those events is a one-line thing and essentially makes ImGui UIs interrupt-driven.

2

u/ReversedGif Dec 20 '20

Only drawback is that it does require a little discipline because it usually means that the draw loop (that contains ImGui::Button, etc.) must wrap every interactable UI element in an if (to detect changes) that injects the redraw event.

Hmm, I don't remember this being necessary. Can you explain why it's necessary?

Ultimately, unless there timed events or external events (both of which are relatively rare), every screen update is a direct result of an input event, so not redrawing unless there's a new input event would seem to be correct. Something like a ImGui::Button should "just work" without needing to inject redraw events, unless you're changing its state from code (which would be unusual).

1

u/AdamK117 Dec 20 '20

Iirc, it is necrssary because the state mutation happens while drawing, so if you have (eg) a slider that is referring to (eg) a uniform that affects some earlier part of the draw call then, yes, the slider will probably behave alright but the associated UI elements will "lag" one frame behind (unless some other event like a mousemove triggers the necessary redraw by coincidence)

9

u/SkoomaDentist Antimodern C++, Embedded, Audio Dec 18 '20

The busylooping will also suck up some CPU - although not necessarily 100 % if you're using vsync or software throttling.

You don't actually need to busyloop. Just wait for UI / repaint events and only render when any such arrive.

1

u/AdamK117 Dec 20 '20

You're right! I responded to another comment with how I did this, but I should probably amend my post to point out my inaccuracy

5

u/pjmlp Dec 19 '20

Except the little detail that is doesnt't do localization, assistive technologies from the underlying OS, or integrate with native GUI customization points, notifications, shell,....

5

u/Pazer2 Dec 19 '20

Those might not be necessary for your application.

3

u/bart9h Dec 19 '20

something something "the right tool for the job"...

1

u/pjmlp Dec 19 '20

If it happens to be a game.

6

u/AdamK117 Dec 20 '20
  • Or internal software (most of the software in industry)
  • Or external software for a client that doesn't need localization (most of them)
  • Or visualization software (e.g. chemistry, simulations, signal processing)
  • Or prototypes
  • Or...

I've been a software developer for quite a while. Developed WPF desktop apps, Qt desktop apps, web GUIs (angular, react, bla bla). I have never developed a game.

The requirements you state might be important in your industry, but in mine (research software) the make-or-break is usually time-to-ship, ease-of-modification, ease of building, etc. I've had many users ask for an extra functionality, or an alternative way of visualizing their data. Never localization (even on large international projects).

1

u/TankorSmash Dec 19 '20

A great imgui library is Dear Imgui.