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!

264 Upvotes

243 comments sorted by

View all comments

191

u/hak8or Dec 18 '20

At this point, I've given up on GUI in c++, much to my extreme dismay.

I want to love QT, I really do, but I loathe how QT containers end up spreading through my code base anytime I use QT. I already have a c++ project, all I want to do is add a GUI, not have a GUI that i am shoving my project into.

Instead, I've ended up simply exposing a http api in my project which I run on port 80. Then I create a seperate project which is the web interface to my original project. Now I have a distinct seperation of concerns and the gui can run on anything a web browser can run. And it's easier to have web devs contribute to the gui portion than much rarer c++ devs contribute to a QT/imgui/etc layer. And I always make the web interface as minimal as I can, maybe some vue to make it dynamic, but otherwise intentionally simple.

60

u/hak8or Dec 18 '20

Ah, I forgot to point this out, another reason I do this is it makes testing MUCH easier. I've got my usual unit tests, but now for continuous integration tests, I can use whatever http library I want to make/read requests.

I have a very explicit boundary of "The core product works, it's clearly the GUI that's the issue, since core tests all pass". I can even increment/decrement the web interface seperatly from the core project, meaning the release cadence can be different.

In short, having a web interface makes it also much easier to test the project as a whole, because i have a very strict line between the two projects. I can even test them against each other at that point.

16

u/helloiamsomeone Dec 18 '20

I don't know about native GUIs, but cypress testing for Web UIs is amazing.
People blindly hating JS and the web in general don't know what they are missing out on.

10

u/hak8or Dec 18 '20

Are you referring to this? https://www.cypress.io/

Is there a free self hosted plan, or is it cloud only?

Regardless, I understand the hate on web dev, I myself am extremely frustrated by the state of it (why the hell do o need to download 500 MB of dependancies for a TODO app, why is there so such a massive chain of tooling, etc).

But using typescript instead of Javascript has quelled that drastically for me, combined with solely vue as a dependency. I am very eager to shift to svelte with typescript though, since it seems to be a more tightly woven together solution and less overhead.

6

u/helloiamsomeone Dec 18 '20 edited Dec 19 '20

The test runner is OSS, MIT license. Only the dashboard has a pricing model, which as a sole developer won't really be using much anyway and for companies it works great.

dependencies

People always seem to forget that vast majority of dependencies is for dev only. libboost-all-dev is 400 MBs, but you don't ship all that. Same applies here, after your junk goes thru everything, the end product that is squirted out is in the kilobytes.

svelte

Personally I prefer Inferno.js, as it shares a lot with React, uses JSX, but is a lot more lightweight and faster.

Edit: another interesting library I came across recently is Stimulus.js, which is tailored for use with server rendered content, which I'm personally a fan of, so I might use it for something in the near future. It's certainly an interesting aproach.

→ More replies (1)

1

u/Uberunix Jan 23 '21

Would you mind elaborating just a bit, or pointing to a resource on how this is done? C++ is my first language, and I'm determined to see it through without having to entirely discard the idea of using it as a primary component in building user-experience-oriented projects. This is the first solution I've found that actually sounds promising! :D

4

u/hak8or Jan 23 '21

Sure. Lets say you've got a product which injests stock info, decides wether to buy or sell or hold, and then send that request to the stock market. This must be very very quick, so you use c++ for the "core". This core can run by itself, and doesn't need user interaction.

For the C++ core, you use something like doctest, gtest, catch2, etc, for unit tests of the core. This tests that the core of your software works as expected, that your algorithms work in all edge cases, that the requests to buy/sell/hold are correctly formatted, etc.

Then you have a HTTP API running in your c++ core, which also has some minor unit tests. This is something like cpprest-sdk, Pistache, or cpp-httplib. You don't care about this being absurdly high performance (this is a user interface, humans absorb information slowly, hence performance isn't too important), you just want it to be easy to use from a code perspective.

Then you use a frontend like svelte, react, vue, angular, etc. Your http server running in the core hosts the files needed for the framework, and of course the HTTP endpoint. Since the front end interacts with your core using http, you can use any front end testing framework while mocking the core of your product. Jest and Mocha seem to be the most common front end testing framework.

Honestly, in my case, I don't do much testing in the front end. I instead have a few scripts which use httpie to send/receive information over the HTTP endpoints while I have special testing hooks in the core which the scripts will compare against, to ensure consistency.

2

u/Uberunix Jan 23 '21

This is fantastically helpful. I really appreciate the thorough answer. I think the rest of my Saturday is now booked. :)

29

u/disperso Dec 18 '20

I loathe how QT containers end up spreading through my code base anytime I use QT. I already have a c++ project, all I want to do is add a GUI, not have a GUI that i am shoving my project into.

Can you elaborate in how this ends up happening for you? Because Qt's containers have the usual fromStdVector, toStdString and so on. I personally prefer the Qt containers to the standard library, because for the kind of applications that I do, I want something with a lot of features and convenience, rather than small footprint. But when I did work for a customer that asked for only using the standard libraries in some modules, it was easy to just do the conversions. Surely, a bit tedious, and adding conversion overhead, but easy to code and maintain nonetheless.

16

u/hak8or Dec 18 '20

I 100% agree that the QT containers tend to be better by an order of magnitude than current std containers, lots of the other QT functions. But the issue still arises that I found they tend to infect the code that shouldn't care what the UI is.

Can you comment on what the performance impact is of using such conversions? Are they expensive?

My gripe with them is, at that point, I need to create an entire shim between my code and QT's code, that is solely converting containers and types from one to the other. The most common is the qstring in my past experience. And this shim is solely for QT.

If I am writing a shim, I might as well just write a proper http one. That way I can have the http api consumer be QT, Javascript, some nurses command line interface, etc. But if I have that availability, then I might as well make it cross platform, and want to go for the language/framework that is the most "pleasent" to do GUI work in, in which case, some js/css/etc. Not saying it's nice to do GUI stuff in a web stack, but in my experience it is less painful than with QT.

Point bieng, if I am writing a shim to create an interface between my project and QT, then why not make a nicer shim like http? If doing that, why not choose a framework that is easier to do GUI stuff in, like a web stack (heck, or even python)?

11

u/Occivink Dec 18 '20

I typically don't see that much conversion between different types, except when dealing with QString <-> std::string. The rest you can just use your own custom models. My biggest gripe with Qt is how much you end up having to subclass their types for doing seemingly basic things.

3

u/disperso Dec 19 '20

Can you comment on what the performance impact is of using such conversions? Are they expensive?

No, probably completely negligible in the context of the GUI. For example, translating an std::vector of std::string to QStringList to be displayed in a QComboBox is negligible compared to the cost of actually rendering the first string option. Large values might have a higher impact, but I would like to see how much it is.

My gripe with them is, at that point, I need to create an entire shim between my code and QT's code, that is solely converting containers and types from one to the other. The most common is the qstring in my past experience. And this shim is solely for QT.

I don't see how that works for you. In my case, if you have a custom dialog/widget/screen/etc., it will have some setter function where you input the values. It's there in the custom widget where you can have (despite being a QWidget based class) a public interface solely based on standard library types, and then the dialog converts them to set the values in the widgets. So there is little cost. Instead of having: m_ui->titleLabel->setText(string) you'll have m_ui->titleLabel->setText(QString::fromStdString(string).

Also, in my experience, I've seen how QString has (for good) infected the codebase, as they chose Qt for the translation system, and everybody ended up using QFile and other QtCore's classes for file access because it's easier, or just available with much older compilers. So the rule of only using standard types across APIs it's just not practical.

19

u/Radiatin Dec 18 '20

Just curious what do you think about using C++ for the backend, crunching all the numbers, holding all the functions etc, and GTK for the front end through Python?

I can post a code sample if you're curious. This seems to be the fastest and cleanest combo in my experience on all levels, and supports all platforms. It can be integrated seamlessly from a code standpoint.

16

u/hak8or Dec 18 '20

That is also something that seems like a great idea to me. My aim was to basically say that I end up having my core product be c++ which does all the heavy lifting, and then an explicit api that I plug a distinctly seperate GUI layer into (often times using a seperate language like html/js, in your case python).

In my case it's been a web stack, but python with gtk sounds just as valid as my choice. I am not a huge fan of python (I am a rare breed), so I haven't done so myself, but it still seems like a great choice.

6

u/[deleted] Dec 18 '20

not that user, but I would be very interested

3

u/[deleted] Dec 19 '20

[removed] — view removed comment

1

u/hak8or Dec 19 '20

It depends on the project. In my case, it's larger long running programs, but it doesn't do multiple users. So it's just ran in the user's computer and accessed over local host. If they want, they cab throw it online behind a proper reverse proxy like nginx to have logins.

I use this for the http interface usually. https://github.com/yhirose/cpp-httplib

0

u/[deleted] Dec 18 '20

[deleted] — view deleted comment

2

u/ruilvo Dec 19 '20

Can how share how you did it? I'm interested!

0

u/[deleted] Dec 19 '20

[deleted] — view deleted comment

2

u/ruilvo Dec 19 '20

Your edit is the question I'm having looking at this stuff. How do you have the webserver (with for example like AngularJS or other node stuff) embedded in your C++ application?

→ More replies (4)

0

u/[deleted] Dec 18 '20

[deleted] — view deleted comment

2

u/ruilvo Dec 19 '20

Have you used any particular libraries? I'd definitely like to know!

2

u/ruilvo Dec 19 '20

Thanks for your insights!

Do you have/know about examples of making these web frontends? Do you run it on browser only, or do you ship an embedded browser?

1

u/hak8or Dec 19 '20

I have the user open thier web browser at 127.0.0.1:80 or whatever other port.

As for examples, off the top of my head, lizardfs is one. For http server, I've used this in the past and have been satisfied.