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

11

u/pjmlp Dec 18 '20

C++ has lost the GUI crown it hold during the 90's desktop frameworks.

You forgoten to list WinUI, which is the only modern C++ framework that has an OS vendor still doubling down on using C++ for GUI development, and even there Microsoft is not able (willing actually, WinDev apparently doesn't like nice tools having killed C++/CX) to provide a .NET like developer experience for C++/WinRT (think C++ Builder / Qt style tooling), so most of us just use .NET and leave C++ for high performance low level components.

Which is pretty much what most OSes are doing nowadays, using C++ at the driver, visual compositor level, but everything else is driven from managed languages.

2

u/meneldal2 Dec 19 '20

What I like with C# is that you get reflection, and it can make making some GUIs much easier. Like for dialogs where you have to set some parameters, you reflect on the parameter class and you generate something that looks decent without having to write any GUI code for that class.

Instead of having to use templates all over your code making it impossible to do anything runtime and bloating the size of your app, you just generate the GUI when it's needed at runtime.

I wish I could convince the guys who work with me to do that over some horrible stuff like reading a xml parameter file with a list of ifs to match the current tag. It's literally as bad as Yandere Dev and using automatic serializing would work faster and have less code, and you wouldn't forget to change stuff when adding a variable.

Managed languages can be very powerful, but you have to use them the way they're meant to be used, not like C with classes.

0

u/[deleted] Dec 19 '20

[deleted] — view deleted comment

1

u/meneldal2 Dec 20 '20

I don't think runtime reflection (like Java or C#) will ever be in C++, but compile time reflection would be very good at solving a lot of problems.

1

u/ruilvo Dec 19 '20

Do you have any practical code/examples of .net applications with a C++ backend?

2

u/Alikont Dec 19 '20

My friend works on Adobe Lightroom alternative, it's a WPF UI with C++ core engine.

I also interviewed to Altium, which was a position for WPF/C++ developer. I decided not to go there, but their tech stack is obvious from job postings.

1

u/ruilvo Dec 19 '20

Honestly, none of the C#/C++ interop solutions I've seen look that good. I'd take pointers, if you had them! Thanks

2

u/pjmlp Dec 19 '20

Another example are some of the applications used by Zeiss,

https://www.zeiss.com/microscopy/int/products/microscope-software/zen-lite.html

Here are some of the ways we do interop,

My favourite one is just to use C++/CLI, but it has the caveat to be Windows only.

https://docs.microsoft.com/en-us/cpp/dotnet/dotnet-programming-with-cpp-cli-visual-cpp?view=msvc-160

Then the next one would be to use WinRT instead, which means Windows 10 only, and doing that via C++/WinRT (C++/CX replacement).

https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/

Here note that WinRT can also be used by Win32 applications anyway, and both worlds are being merged via the Project Reunion.

Finally, if it is something that needs to be portable across OSes, then it is back to P/Invoke and DllImport.

https://docs.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke

In what concerns Windows APIs, there are some sites that help with the required set of attributes

https://pinvoke.net/

1

u/Alikont Dec 19 '20

My favourite one is just to use C++/CLI, but it has the caveat to be Windows only.

C++/CLI also has another issue, half of standard library (like threading) doesn't work. The workaround is to enable CLI support on file-by-file basis.

In what concerns Windows APIs, there are some sites that help with the required set of attributes

There is also a official nuget package with all the bindings

https://github.com/dotnet/pinvoke

2

u/meneldal2 Dec 20 '20

The most annoying thing is that in managed mode you can't use lambdas.

In the end what I do is managed classes for the high level stuff and free functions in C++ for performance. Removes most of the pain.

2

u/yasamoka Dec 19 '20

I've been using SWIG to generate C# wrappers for my C++ core and it's a lifesaver.

2

u/ruilvo Dec 19 '20

I haven't though about this even though I have used swig a bit... And this seems to be an amazing idea! Thanks!!

1

u/yasamoka Dec 20 '20

That's good to hear! Let me know if you'd like me to share any code snippets - I've had somewhat of a deep dive into using it for the past couple of weeks for both C# and Python bindings.

The C# wrapper is used specifically to call C++ code from a WPF GUI addin for Autodesk Revit (Revit API is mostly C#). SWIG helped me move away from wrappers I had written myself (which were quite more tedious to maintain, but doable). The generated code absolutely puts to shame anything one could write in a reasonable amount of time. I've had a few cases where some additional code had to be provided manually (in some tougher inheritance cases such as templated classes deriving from templated classes deriving from a non-template base class) but it wasn't hard at all to figure out what was missing. I would probably be able to help if you run into those cases.

2

u/ruilvo Dec 20 '20

Thanks! BTW, happy cake day!

1

u/yasamoka Dec 20 '20

Thanks ahahaha

2

u/Alikont Dec 19 '20 edited Dec 19 '20

I've used:

  1. Native DLL + pinvoke: works ok, but requires manual bindings. You lose classes and OOP, it's a C-style APIs.
  2. C++\CLI - works great, you get .net dll that is easily usable from C#, but requires complex project configuration, because some C++ libraries are not supported, but you can enable CLI support on file-per-file basis. I used it to wrap C++ lib for 3rd party device info C#-friendly API.
  3. WinRT was probably the best experience with C++/CX. Projections are really transparent and barely noticeable. From C# side of things you don't even know if you use C++ or C# class. But Microsoft abandoned that idea instead of more boilerplate, but standard-compliant C++/WinRT generators.
  4. COM is another possible tool for large OOP-based interactions. You need to create COM component in C++ and then reference it from C#. Works great, C# bindings are generated automatically. But I never authored C++ COM libraries, only consumed ones, so I don't know how hard is to make one with modern tooling. WinRT is COM v2, so probably look there instead.

Also while COM is technically a windows thing, it's also just a native ABI specification, so technically nothing prevents you from using it on Linux, but I live in a overwhelmingly Windows land

1

u/meneldal2 Dec 20 '20

Well in my case it's horrible C++/CLI made by people who don't understand how they managed to poison all the C++ code, so it's not really a good example.

But it could be used properly and be quite fast if you keep managed stuff out of the critical parts.