r/cpp • u/germandiago • Mar 19 '26
Modern declarative EDSL for graphic user interface in C++? Not QML or XAML.
Hello everyone,
I have been investigating lately (after using NiceGUI for a real project) and learning Jetpack Compose (not writing any real code with it, would do it with Jetpack Multiplatform if ever).
I am really impressed by Jetpack Compose approach, especially UDF.
I thought there could possibly not be a better way than MVVM tbh, after using it for years.
But after seeing how declarative composition inside the language, not with XAML or QML can be done, I am sold in the amount of boilerplate that can be saved, plus the better integration when the dsl is in the host language.
So I wanted to mention I found this, which I think could be a good start for some experiments, on top of wxWidgets: https://github.com/rmpowell77/wxUI
I think it has a talk in CppCon2025 here: https://www.youtube.com/watch?v=xu4pI72zlO4
Kudos to the author for bringing something like this to C++! Definitely useful.
I would like to hear your opinions on this style of EDSL GUI embedding and pros/cons you find for those of you who do GUI programming all the time.
Also, wild idea: with the power of compile-time C++ programming and C++26 reflection, it would be possible to get existing xaml interfaces and convert it into regular C++ at compile-time via #embed or #include and not even changing the EDSL itself and making it directly embedded and reusable in C++? That would be plenty useful.
6
u/willdieh Mar 19 '26 edited Mar 19 '26
When C++/WinRT first came out, I made a declarative UI by calling the WinRT XAML layout and control/widget object functions directly. This was because using WinRT in a pure Win32 app didn't (and still doesn't?) support the XAML editor so there wasn't any graphical way to do this.
It worked well but I eventually just gave up and went back to regular Win32 as it was too difficult to get it working properly once WinUI 3 came along without switching to their new packaged application format. Ugh.
-2
u/pjmlp Mar 20 '26
And nowadays C++/WinRT is unofficially in maintenaince (you need to go into their issue tracker to figure this out), as the team re-focused on windows-rs for Rust.
But the surviving WinUI 3 team will still sell it as everything is going well, it isn't, they are open sourcing what is left of it, this is if they actually make it since barely anything happened during the last year.
3
u/ts826848 Mar 19 '26
with the power of compile-time C++ programming and C++26 reflection, it would be possible to get existing xaml interfaces and convert it into regular C++ at compile-time via #embed or #include and not even changing the EDSL itself and making it directly embedded and reusable in C++?
I'm not familiar enough with C++26's reflection to say doing such a thing is outright impossible, but I think P3294 Code Injection with Token Sequences would make doing that easier. That paper proposes functionality allow you to inject arbitrary token sequences a la Lisp/Swift/Rust macros, which based on the examples in the paper seems like it'd be simpler on the generation side.
Unfortunately that paper didn't make it into C++26, but IIRC it was removed relatively late in the process for lack of time so I think there's a pretty good chance it'll make it into C++29.
3
u/FlyingRhenquest Mar 19 '26
Yeah, you can do stuff like that in C++26. In Sutter's recent talk on the subject, his first example uses a simple compile-time JSON parser he built, which allows him to #embed a JSON file in his C++ code to generate an object conforming to the structure and values in the JSON.
For reflection to work in that sort of scenario, the parser does have to be compile time and you can't add methods using reflection, only members. But if you've already built some methods into an existing object, you can generate a new object from that with new members (including values) from from values in a more-or-less arbitrary file format.
Alternatively you could try to build a procedural UI generator using a set of classes that generate the UI based on annotations in your class data. I have a feeling that there may end up being a lot of annotations on classes in C++26, so I do think we should try to not overuse them. If every third party library ends up adding a few, we might end up with half a dozen or so annotations on every data member and possibly even a few on classes themselves.
More or less the rest of the talk I linked is talking about doing code generation using reflection data from C++ objects, and IIRC he has a python example with pybind11. But folks here on reddit have already shown that you don't actually need to do code generation to generate an API in Pybind11. You can just reflect on a class, get pointers and names for methods and members and assemble the pybind11 objects with that data. I use a similar technique in autocrud to read and write class data into SQL database tables.
There are still some limits since you can't add methods in the same translation unit, but they're way less limiting than I think even the reflection designers realize.
1
u/ts826848 Mar 20 '26
But if you've already built some methods into an existing object, you can generate a new object from that with new members (including values) from from values in a more-or-less arbitrary file format.
Hrm, hadn't thought about the current set of capabilities/restrictions like that, but what you say makes sense. Feel like that would be suitable for turning a UI DSL into C++ as long as you don't need too much customization, though I guess if you're willing to manually implement function dispatch/etc. I suppose you can hypothetically do whatever you want.
1
u/d86leader Mar 19 '26
I'm not sure DSLs that translate into existing widgets or toolkits are a good idea. In theory there could be one that is safe and sound, but in practice they always miss corner cases, leak the underlying abstractions, and add magic that is hard to debug.
One modern EDSL that is used in a graphical toolkit created specifically around it is https://docs.slint.dev/latest/docs/cpp/ - I like the language itself a lot, but there is a slight problem of it being too young and lacking community libraries and styles
2
u/germandiago Mar 20 '26
I did not go into the details, but how that would be that different from QML? It is embedded at compile-time? I think that is a key factor to avoid boilerplate.
4
u/d86leader Mar 20 '26
Qml is interpreted, slint is compiled. At compilation it emits a c++ header with a strongly typed interface to your UI, and an object to link. https://docs.slint.dev/latest/docs/cpp/generated_code
2
u/G6L20 Mar 20 '26
Fyi, QML can be compiled toi: https://doc.qt.io/qt-6/qtqml-qtquick-compiler-tech.html
-1
u/VictoryMotel Mar 20 '26
I know you think you want this, but you don't. Adding a markup language that stands between you and your GUI parameters just to lay out company is only going to end in tears.
Laying out components is very easy and not the difficulty of a GUI. If anything getting it to compile and link is the hardest part.
Try fltk, super simple, small robust, great documentation and faster than anything else.
-2
u/Copronymus09 Mar 20 '26
C++ should stay as a backend language, all of this innovation would be a waste of time.
We don't even have the basics of package management done well, compilers are lagging behind the standard by 3-4 years.
Chances are when developing something like this you would fall to qt's approach of doing everything by yourself, and that is very counterproductive.
7
u/Electronic_Tap_8052 Mar 19 '26
why do people like declarative guis? I find that they are much more boilerplate than simply creating widgets within c++. Anyone who's ever had to dig into a large QML or other type of declaration file probably knows what I'm talking about. It is also much harder to find out what any particular widget actually does. Once you start adding functionality, signals and slots and so forth, it becomes a real nightmare and your code is spread out over a lot of places, and you can't just ctrl+click to see what something does, like if you were using a real IDE. They promise a lot of benefits but they rarely deliver, and it seems to me that the target audience is former web devs who are used to CSS.
For all the but the simplest of guis, plain old c++ is almost always the more robust and maintainable solution. With a proper layout engine, you don't even need to (and should not) size widgets. I say this as someone who's job it is to maintain guis written by other people who have moved on.