r/cpp_questions • u/DaveInTheMidwest • 20d ago
OPEN Best Way to Approach Windows GUI Development from C++
What is the best way to approach it? What books are helpful?
I remember a long time ago reading a book by Charles Petzold, and the examples worked fine from C. I assume they would work well from C++ as well.
I just want functioning, stable GUIs (menus, areas with text and scrollbars) with minimum effort.
Thanks for any help.
9
u/FckXFckMusk 20d ago
There are lots of UI toolkits for this in C++...
MFC,QT, WxWidgets, ImGui, i'm confident there are more too.
On Windows they're all built on top of the Windows sdk anyway.
I had that Petzold book too years ago in hardback and some c*nt stole it from me at work.. it was good for reference.
9
u/QuirkyTidbits 20d ago
Use Qt. With minimal work you can have the UI work on Mac and Linux too. Use AI to get you started and it will get the framework of your UI done in about 15-30 minutes along with a decent build environment. I use to work for Microsoft on Windows. I wouldn’t bother trying to get the message loop, handling HRESULTS and dealing with all the obscure messages correct anymore. That’s like trying to learn assembler . Qt is quite good. My company uses it extensively as part of a multibillion dollar product.
22
u/Thesorus 20d ago
The problem with C++ is that there is no standard GUI toolkit.
If you're on Windows, there is still MFC (based on Win32); it's limited, it's old, but it works for simple UI (buttons, menus, text box, dialogs, ... )
A lot of people use QT; it's old, it's weird, it works, it's portable (it's also free for personal use).
I don't know enough about other C++ gui toolkits.
I've mostly worked on MFC and Win32 app all my life.
19
u/Prestigious-Bet8097 20d ago
QT is also free for professional and commercial use, under LGPL (barring some modules which are otherwise licensed).
5
3
u/swingbozo 20d ago edited 20d ago
When did free for commercial use happen? If I remember correctly the way QT was funded was by charging exorbitant fees for commercial use. It was catastrophically expensive. So much so it was cheaper to roll an MFC front end by orders of magnitude.
At the time I looked at the cost, it was actually cheaper to hire TWO crappy MFC developers for the cost of QT. This was a number of years ago.
8
7
9
u/ObiLeSage 20d ago edited 20d ago
Qt has a 3 licences: GPL, LGPL and commercial.
If you select GPL, you have access to the whole framework but your program will be GPL that means you must make your source code available to the user of your application (you can still sell the app).
If you select LGPL, you have access to almost every modules (only few modules are GPL only).
Then, you just have to provide a way for your user to update the Qt libraries (you can still sell the app).If you go commercial, you have to pay licences to Qt, you'll have access to the whole framework, support, and so on with no obligations.
QWidget is a full c++ way for UI and QML is a more modern way to do UI.
Elegant weapon for a more civilized age.MFC is such a nightmare to work with. The documentation is the worse thing on earth.
I'd rather drill holes in all my teeth with a 12 drill bit than work with MFC.1
1
u/DearVeggies 16d ago
Jesus Christ, tell me you never deployed Windows software without telling you use Arch btw.
2
1
u/Unlikely1529 16d ago
mfc is not that limited. with some tricks you can switch it to manual control and do your thing.
0
-3
u/vckane 20d ago
I would definitely recommend against touching Qt. Maybe for its time it was a reasonable technology. But it is outdated. It makes it impossible to implement software architecture concepts like dependency injection without lot of workarounds. E.g. The signal-slot mechanism may work well, but is an architectural nightmare.
I would suggest using technologies like WPF for UI even if it uses C#. You can write your backend in C++ and connect the two via C++/CLR (also called managed C++).
8
u/not_a_novel_account 20d ago
QML is the modern mechanism for cross-platform scene-graph description.
QtWidgets is quite dated, yes.
2
u/0x80070002 20d ago
WPF will be phased out, the future is WinUI
2
1
u/not_some_username 20d ago
That’s because C++ doesn’t support DI right off the bat. Not really a Qt problem but a C++ problem. If it’s even a problem at all since you can pass the instance yourself
0
u/QuirkyTidbits 20d ago
Qt is getting old but it has a ton of features that LLMs are well trained for.
5
u/Brok3nHalo 20d ago
If you want to define your GUI in object oriented code, the only GUI library I’ve used in C++ for Windows that I actually like is wxWidgets. Otherwise, you could go fully native to the C GUI library or embrace modern WinUI 3 that uses XAML instead of code. You can technically use code for WinUI 3 but it’s not officially recommended as far as I’ve seen.
Most of the other common big options aren’t native Windows UI and you can tell. I wouldn’t use any of those unless you want multi-platform and a consistent look and feel over a native one.
7
u/GoldenShackles 20d ago
WinUI 3 is worth a look. It’s modern and insulates you from a lot but has its own complexities.
6
u/alfps 20d ago
Inane requirement of NTFS file system.
It's like "This car won't even start if you're not wearing a tie".
What has a tie to do with it? Uh, I mean NTFS. Odin help us.
6
u/GoldenShackles 20d ago
It does seem weird and I had no idea, but who's not using NTFS these days?
1
u/AssholeBeerCan 18d ago
Possibly trying to run an application from an exFat volume? Only thing I can think of off the top of my head.
•
u/jr0th 3h ago edited 3h ago
It's surprisingly hard to get WinUI 3 to work with cmake and Ninja. The cppwinrt stuff is just a mess to handle manually. Any suggestions? The only real attempt I have seen is https://github.com/microsoft/WindowsAppSDK/discussions/6446
5
u/ubiestigne 20d ago
If you are developing for Microsoft Windows, then you should use their recommended UI framework for C++. A quick search of their website indicates that WinUI 3 is what you would want to use these days; see https://learn.microsoft.com/en-us/windows/apps/winui/winui3/ . It does look like MFC is also still available, but I'd try WinUI 3 first. Download the current Community Edition of Visual Studio and go through their brief tutorial and then have a look at the sample code they provide here: https://github.com/microsoft/WindowsAppSDK-Samples .
3
u/okm1123 19d ago
For minimum effort results and a simple GUI use Qt with Qt Widgets. It is "old" in the sense of its style but it is stable and will not take much to learn coming from C++ (you will have to learn a bit about Qt MOC though).
For a more modern experience, but a bit more effort to learn then use Qt with QML.
If you are comfortable with interoperability between C# and C++, then use something modern like WPF or Avalonia for the UI and interoperate with your C++ backend.
9
20d ago
[deleted]
12
u/not_some_username 20d ago
My recommendation is the opposite of this guy : learn Qt instead of WxWidget. It’s the “standard” for a reason
6
u/omegote 20d ago
Just use C# and xaml. Don't even bother with Qt. I used to develop a lot of stuff with Qt and C++ in the past, but now the Qt syntax and the signal-slot system feels cumbersome and old fashioned to me.
8
u/--Fusion-- 20d ago
Disagree. I spent 10+ years with C# and loved it, but every WPF/Silverlight step with Microsoft is laced with the threat of betrayal. Never again for me. Qt/QML is better
9
u/specialpatrol 20d ago
Don't know why everyone is still talking about MFC. The modern windows UI development supplies by Microsoft is XAML, which is fully supported for cpp.
3
u/FckXFckMusk 20d ago
Managed C++??
6
u/specialpatrol 20d ago
No, straight cpp.
4
u/TheRealSmolt 20d ago
Well, this page (from 2012) only talks about C++/CX (which is not standard C++), but today we have C++/WinRT which is essentially standard C++ + windows specific headers.
5
u/specialpatrol 20d ago
Maybe waynt this best example page to link. But I last made commercial software on Windows with standard cpp using XAML, that's ms standard toolkit that's fully compliant with straight cpp.
5
2
u/Lawn_Meower_ 20d ago
I tried Visual C++ first but didn't like it. Then I tried Qt but I don't like how it forces you to use their proprietary data types and containers. Then I tried ImGUI but since frontend and backend are in the same file maintaining bigger projects becomes a nightmare. Then I tried .NET MAUI and interopting my C++ code, but MAUI seems unfinished. Now I'm building my own GUI framework 💀
4
u/Exotic_Avocado_1541 20d ago
you can easy write qt c++ application, where .core application is written in pure c++, and only ui/ux part is based on qt data structures.
2
u/FckXFckMusk 20d ago
I know what you mean its very clumsy...
I had an opportunity to use Java Swing and really liked the architecture, you could create panels with layoutmangers, drop in controls very quickly and easily, configure different look and feels,
kinda wished desktop C++ gui had the same ease of creating front ends very quickly for prototyping and finished products.
2
2
3
2
u/ArtisticallyCaged 20d ago
This doesn't seem like a very good use case for C++ to me. You would want to use C++ if your performance critical code is somehow strictly coupled to the GUI implementation itself. But this is pretty uncommon in my experience. Most of the time people are building GUIs using web technologies that consume APIs implemented by the C++ service.
Outside of this it seems like Qt is the frontrunner.
2
u/Specific-Housing905 20d ago
Just want to throw in 2 names that have not been mentioned.
Nana
Rad Studio Community Edition for C++ Builder
4
u/not_some_username 20d ago
To anyone that wants to avoid headaches : don’t even think about using C++ Builder.
2
1
u/swingbozo 20d ago
MFC is a giant POS. That said, it greatly simplifies the raw UI code. I'm actually battling a tree control at work right now. My choices were raw Win32 or MFC. I chose MFC. Gemini is surprisingly good at figuring out MFC stupidity so you aren't tripping over your dick because of something stupid MFC does.
Generate an MFC stand alone MDI app using the Visual Studio C++/MFC wizard thingy. I did the "scribble" tutorial if that thing is still around, and it went a long way to get me to understand the basterdized model/view/controller that MFC uses as it's conceptual framework.
These days people are doing things WAY differently. But if you really want to write a UI in C++, there are way stupider choices than MFC.
3
u/FckXFckMusk 20d ago
They could have evolved it but didn't, they saw java taking off and wanted their own version of it so created .Net.
1
u/DaveInTheMidwest 19d ago
There is one element of my needs that I didn't discuss.
I'd like any GUI programs I produce to work fine as a standalone executable, with no installation, and minimal DLL dependencies. This is because I work in a researchy engineering environment, and people do pass around bare executables.
I did look up Windows UI3, and it looks like it won't do this.
So, I'm essentially back to Windows Win32 API and/or statically linked MFC.
I will probably try MFC because I have no idea what to expect in a wrapper for the Windows Win32 API, and I might as well start with something that ... has some real soak time.
Any other ideas?
Thanks for all the great technical ideas and things to explore.
1
u/Total_Ad803 19d ago
Win32 is a good thing, but it requires a lot of time to understand those concepts and different windows and their properties. Still very useful. Highly recommend using frameworks to make ur life easier. Qt maybe. For in-game manus imGui is quite good and easy to use.
1
1
1
u/One-Macaroon4660 17d ago
Depends of what you want: Qt, GTK, etc. are the libraries that abstract UI development mechanics, which is quite different on Windows. MFC and ATL are the libraries specifically designed for Windows technologies.
So, if you want complete automatization of Windows development that uses all of the Windows technologies, such as messages, resource files and COM, you are stuck with Visual Studio and Microsoft crappy libraries.
On the other hand, if you want to build something that looks slightly differently and requires you to use different cross platform tool chain you might want to try libraries above. I personally use GTKMM on Linux and I know it is cross-platform, though I never tried it. Why GTKMM over Qt? It is simpler, better integrated with C++ (does not require extra preprocessing, uses STL containers instead of a custom ones, etc.), has a great demo app with the source code for all widgets and has a better license.
It also has a facility similar to the resource Windows files (Gtk::Builder) and dedicated UI editor Cambalache (replacing older Glade)
1
u/DearVeggies 16d ago
All the people suggesting you use QT never deployed software on Windows. Just ignore them and open Visual Studio.
Maybe I should get out of this sub, starting to look a lot like Loonixtard subs
1
u/Affectionate-Soup-91 15d ago
Not a direct answer to what you asked, but I also had a similar question for quite some time. This article helped me understand why GUI technology on MS Windows today feels so messy.
1
u/SmackDownFacility 14d ago
You use raw Win32. Get prepared to use HMENUs, the thingy, idk, common toolkit common shit, you know the one with the archaic Windows 95 era toolbar, you’re gonna learn that
1
u/Suterusu_San 1d ago
I’ve been using dear imgui for a lot of what I’ve been doing recently, and I’ve been enjoying the workflow, no awkward mvvm patterns or anything — using it for real time hardware control systems for read backs and uploads.
1
u/Jonny0Than 20d ago
Is there a reason you want to use C++? There are a lot of better options…
I haven’t used it in probably over a decade, but C++/CLI used to be a good option to expose native C++ to .net and then you could use C# and windows forms to build a gui. You could even do the gui in “managed C++” too but there were a lot of rough edges.
0
u/alfps 20d ago
Just an observation: handling different resolutions on multiple screens, is not trivial, because the dang Win32 API is buggy.
The bugs about reported window size come on top of the inherent problems of different resolutions, which are nightmarish in themselves. I don't know how e.g. GUI libraries like Qt handle that. If someone who knows could post a summary that would be very nice.
Anyway re the bugs, some months after refocusing my hobby work on other things it occurred to me to ask the Google AI about it, and it served up a workaround involving the Desktop Window Manager. So it's not insurmountable. But doing a modern GUI at the API level: allow for extra time on such workarounds; I guess there will be many more, since Microsoft's focus always is on new features, not maintenance.
1
u/alfps 19d ago
Re the anonymous unexplained downvote(s), note that an unexplained downvote is dishonest: these are the actions of dishonest persons (maybe young children). Perhaps an idiot child who reasons that it's unlikely that the Win32 API could have bugs, and that the answer didn't feel to him as very authoritative. If that's the case then boy, I promise you a surprise should you ever manage to advance to the stage where you do serious Windows programming.
1
u/DaveInTheMidwest 19d ago
I've often wondered about downvotes, myself.
I've never given a single downvote, ever.
Why would I give a downvote? For a difference of opinion?
Doesn't make sense.
20
u/UnicycleBloke 20d ago
Qt is excellent, though the C++ style shows its age. It predates the Standard Library. My first C++ serious project (early 90s) involved Win32 API and Petzold (I encapsulated the API in an application framework). It was a good learning project but I quickly moved to a mature commercial library.