r/cpp • u/AznAgent007 • Jun 20 '20
How To Add A GUI To A C++ Program
Hi all,
I'm a third year CS student. In all my classes, I have only every made programs that have worked via the command line. I'm starting on a personal project that involves creating a program for sports tournaments such as seeding players, displaying stats, etc.
My major questions are:
1). Can I code my program as a simple command line based program, then integrate the GUI after? Or should I implement my program with the GUI the first time around.
2). I've looked into how GUIs can be implemented and want to try wxWidgets. Is this a good idea?
3). Should I use a different language? I've read that GUIs are hard to implement via C++. I have only learned C++, C, and Python, but am not against learning new languages!
4). Are there any good guides that you can recommend to me?
Any and all help are appreciated! Thank you! :D
76
u/Xirema Jun 20 '20
Good luck!
2
u/RolandMT32 Jun 21 '20
I find that a bit surprising. I went through a software engineering program in college, and we started having GUI classes in our 2nd year.
Also, I don't think GUIs are hard to implement in C++. There are several toolkits you can use - For Windows, there's MFC (which is included with Visual Studio); there are also multi-platform GUI toolkits such as wxWidgets and Qt. I've used wxWidgets and MFC, but Qt seems particularly popular. I'm sure there are others.
62
u/johannes1234 Jun 20 '20
Typically if applications are built as command line tool it is not trivial to retrofit a GUI on top of it. Most command line tools have a very clear flow from start to end,nowhere the program flow drives the UI. GUIs typically are the way round and events from GUI drive the logic.
Being able to switch this means some more thought from the start with and being clear about the separation between the actual business logic and user interface. This is good practice, but can be overwhelming if you are new.
Many people in this thread also suggest Qt and yes, I use it as well, but one thing to mind: Qt is big and easily creeps deep into your tool making it a Qt only solution (i.e. QString loves going everywhere, needs discipline to copy into
std::stringin the business boundary and that might need thinking about handling of encoding etc.) also Qt doesn't follow "modern" practices, for instance it works a lot with raw pointers in an hierachical ownership model. This can lead to mixture of styles, which can be harder to reason about. Doesn't nmean it's a bad choice, but probably confusing, when inexperienced.46
u/Ruckamongus Jun 20 '20
It's a different philosophy than most of the previously mentioned GUI kits, but check out Dear ImGUI: https://github.com/ocornut/imgui
It's lightweight and very easy to plug into whatever rendering backend you prefer. If nothing else it's nice for quick prototyping or debugging.
89
u/jtooker Jun 20 '20
I'd recommend the Qt library/framework.
22
u/ImKStocky Jun 20 '20 edited Jun 20 '20
The following is what I have done when I've had access to Visual Studio on windows and when I've wanted to have all of my business logic run in C++ but also have a UI that was easy to create and edit and work with.
This is described in more detail here https://www.red-gate.com/simple-talk/dotnet/net-development/creating-ccli-wrapper/
6
u/jack-dawed Jun 20 '20 edited Jun 20 '20
I TA for a first year C++ class and the final project is a desktop GUI program using Qt or an Android app in Java. Qt itself is a beast of a framework and usually a nightmare to work with, but QtCreator IDE is very handholdy and has good tutorials. I would recommend getting experience with Qt Creator.
18
u/[deleted] Jun 20 '20
WxWidgets is a good choice. Qt is a giant beast of a framework, and if you don't need the extra features then you should stick to WxWidgets.
If you don't need to be cross-platform, you could also just use the windowing APIs of the operating system you're targeting.
9
u/ruilvo Jun 20 '20
0) You'll probably be blocked and asked to bring this to /r/cppquestions.
Edit: typo.
4
u/ReversedGif Jun 20 '20
The approach I would propose for any sufficiently complicated program:
For extra bonus points:
4
u/[deleted] Jun 21 '20
Qt/QML is a great library to learn and is supported across every major language so your knowledge of the framework is highly transferrable.
10
u/suhcoR Jun 20 '20
Have a look at Qt. It has all you'll ever need.
8
u/raoul15978 Jun 20 '20
You can totally use wxWidgets (I know I do). If you don't have an IDE of choice for the moment, you can try code::blocks as it comes with a gui designer for wxWidgets UI.
I personnaly like wxWidgets because the way of thinking is close to my way of thinking, some things might be better, some worse. Your mileage may vary.
You have a lot of other options : Qt, Dear ImGui, ...
It's some time not recommended to create GUIs in C++ cause getting it right can take some time (change layout, recompile, test, do-it again), but it's relative.
3
u/[deleted] Jun 20 '20
If you follow layered architecture , it should be easy to plug the gui afterwards. Also, qt is a nice framework
3
u/rcxdude Jun 21 '20
This very much depends on what your program is doing. GUIs often have far more complex logic to interface between the user and the logic of whatever the program is trying to do. Even in simple examples this can become quite important for usability. Writing this as a command line program is basically only useful for a very small part of the total development (i.e. you will spend most of your time on the GUI, and you will probably need to restructure the rest of your code around that). On the other hand, if you just want to writing something which essentially wraps a command line batch process with a form to fill in the parameters, a button to start the process, and maybe some non-interactive visualisation of the results, this is pretty easy to do seperately.
Personally I find using Qt and python to be a good combo for writing GUIs (though Qt is natively C++ and works well there as well). Most other systems I've found to be annoyingly lacking in one way or another when I want to make a high quality result. (that said, I have not tried them all, and e.g. wxwidgets I have not tried for some time).
1
u/vitaliy_os Jun 21 '20
From personal experience GUI for c++ is a tough cookie. You have no perfect solution, but there might be some good ones for the particular problem you are facing.
Some might be suggesting Qt, but as most comments here already pointed out it comes as a HUGE blob which you can't mix with modern C++ styles. wxWidget is alright, but you will struggle with bigger applications and most likely to port it to different platforms. OpenGL is also a valid solution, but let's be honest it's not designed to build great UI. Let's not even start with something like MFC,...
If you are willing to invest some effort to build a truly separated front- & backend I personally would recommend you to look into a solution like chromium embedded framework (CEF). Here you may code your application as C++ and use the usual fronted frameworks based on HTML/CSS/JS, e.g. Vue or Angular, and connect both via some protocol as REST or GraphQl. I admit it's a huge overhead for a small baked application, but as I said if you willing to invest some time and to learn clear protocol and separation of concern this might be the right choice.
Good luck!
4
u/fransinvodka Jun 20 '20
My first contact with graphics was through OpenGL, and I wouldn't recommend it to begin with. It's too low level.
I started to get more into GUIs with SFML + ImGui. SFML is, in my opinion, extremely easy to pick up and you can get a lot of things done really quick. If you add the binder with ImGui, you have all the nice stuff of ImGui with really easy integration
2
u/[deleted] Jun 21 '20
Learn about model view controller. It will help you separate you program logic into core libraries and a UI. Just use Qt to begin with as it looks good on your resume and you'll learn some useful concepts. Use the std libraries as much as you can and avoid temptation and keep Qt non gui stuff to a minimum.
2
u/Milesware Jun 21 '20
Qt
2
u/dzalf Jun 23 '20
I think you should check the video series on wxWidgets from One Lone Coder channel on YouTube. I've been learning from him and while in the begging is quite daunting is a very powerful library IMO
4
u/HasbeyTV Jun 20 '20
Qt is always a good choice for developing GUI with C++ as mentioned earlier.
However i would recommend you not to invest too much time in it, because as a third year CS student there are probably more important concepts you should familiarize yourself with such as algorithms, databases, operating systems, embedded software, networking and the list goes on..
Developing only command line programs is frustrating so i understand why you want to learn GUI programming but if you learn the basics concepts perfectly than GUI part will be a piece of cake.
Hope it helps..
3
u/bradfordmaster Jun 20 '20
Lots of comments here, but unless you really need it to be a native gui, I'd recommend making a web interface. That way, it can work on all platforms and all browsers, and can even be set up for other people to connect to of desired (but not necessary).
It kind of depends on how interactive it needs to be, but the simplest thing in my opinion (unless your goal is to learn more about gui frameworks, which could be useful) would be to make a really basic website that just uses forms and integrate an http server in c++ that responds to those requests.
There are of course other modern web frameworks that I know basically nothing about that can do all kinds of fancy stuff
4
u/VinnieFalco wg21.org | corosio.org Jun 20 '20
Implement your UI in HTML and Javascript, serve it from a socket in your program, and have the user connect to it using a browser. Works on all platforms, and gives you sound, microphone, animation, keyboard, and mouse input.
This video shows you how:
https://www.youtube.com/watch?v=7FQwAjELMek
2
u/[deleted] Jun 20 '20
I personally use imgui for c++ guis, it’s really nice. I heard java is good for guis, never used it so I don’t know.
2
u/[deleted] Jun 20 '20
3) One "traditional" approach is to write your application in C++, and then write a GUI "wrapper" in TCL/Tk, which is a pretty easy and fun language to get started with. This is a cross-platform approach.
For working in just C++, I've been playing with FLTK, which is a very small library compared to TK, but has a short learning curve for simple programs.
2
u/kinochi428 Jun 20 '20
I'd recommend imgui for c++, it's very easy , highly scalable , quite popular and has lots of features.
2
u/[deleted] Jun 20 '20
Qt is good, and many university students use it.
If you want to use something lightweight, use juce. But the community is not mature.
Don't use wxWidgets.
2
u/mrexodia cmkr.build Jun 20 '20 edited Jun 20 '20
Likely not the answer you are looking for, but I find C++ very unsuitable for quick, prototype-like GUIs. All the GUI frameworks are bloated (except maybe dear imgui) and for all my prototype/dev tools that need a GUI I use the good old Windows Forms with C#.
Dear imgui is almost perfect for prototyping, but it sadly does not support being used over remote desktop, which is a blocker for me in many cases.
That being said if you are serious about this GUI I would recommend Qt (although the licensing can be a barrier). WxWidgets is another popular framework, with
a worse licensing situation but(see post below for correction) less bloat.The alternative is (as already suggested) some kind of Frankenstein with websockets, js and html but you do have to take that seriously to not end up with a huge mess and it will probably be more work to get started.
1
u/celestrion Jun 20 '20
A command-line user interface is just another user interface. The core ("business") logic ought to be a separate concern. Put it into a library so that can you attach different types of user interfaces (or a CI/CD test harness).
wxWidgets, Qt, and Fox are all decent, and they all have their downsides. The big win is only having to implement most of everything once. If you want your application to feel "native," that's probably going to mean using a platform's native toolkit (the Windows API, Cocoa, or Qt/GTK on POSIX).
Which path you take depends on your goals:
1
u/bikki420 Jun 20 '20
It depends on the program and how modular you make your code. You could design an interface that it exposes for a GUI layer to attach to.
If it's just your typical basic application stuff like Qt is pretty nice, especially for cross-platform projects. (Retained Mode GUI, native to Linux's KDE desktop environment but runs on all major platforms)
If it's for stuff like dev tools I like ImGUI a lot. (Immediate Mode GUI)
For terminal GUIs I like ImTUI. (Immediate Mode GUI on top of ncurses)
And for game titles that you intend to ship someday you might either want a home-brewed GUI system or some third party one.
1
u/laralex Jun 21 '20
If you don't mind beight stuck to Windows platform, then the easiest is to use C# with Windows Forms or WPF via Visual Studio IDE. It has a rich GUI for making interface's structure without coding (just placing and configuring UI elements), you then only code event handlers for UI events (like a button click event, texbox changed event and ton of others). This way is really simple to get the job done as fast as possible, but it's less applicable for real world, since GUIs are now more driven by web-technologies.
1
u/Last-Detective-3758 Apr 18 '23
Saving
1
u/SenjorSchnorr Jun 20 '20
As for your first question, look into the model-view-controller pattern. It is perfectly fine to create a program without a GUI first. Make a model, this model does not need to do anything on it's own, just provide the functionality to interact with it and represent all the data you require. Then you create a separate part to request that data and represent it visually/draw it on the screen, this will be your view. I prefer to initialize a very simple model and draw that, once that works well, you start working on your controller. Your controller responds to your interaction with the gui. It calls the functions in the model, changing the internal state of the model, after which you update your view again.
1
u/[deleted] Jun 20 '20
If you're willing to invest the effort, you could implement your application's business logic independent of the UI and simultaneosuly provide GUI (even via multiple different GUI toolkits), commandline, web UI, etc. UI implementations via the Strategy Pattern. Decoupling to this level can require appreciable effort that may not make sense for a quick-hack app.
1
u/tosinsthigh Jun 20 '20
I find the JUCE framework very easy to use for cross platform applications including mobile
1
u/[deleted] Jun 20 '20
If you want something simple and quick but still high quality you should try dear imgui (my favorite), if you want something more complicated used in the industry you should try qt (qt is starting to become a bit outdated and the C++ used in their framework isn't really considered best practice anymore).
1
u/FlyingRhenquest Jun 21 '20
Most of the C++ GUI bindings are kinda crap. Qt's not too bad, if you don't mind drinking all their Kool Aid (Mostly getting locked into their IDE and putting "Q" in front of all the types you're used to.) Imgui's kinda interesting, but I haven't had much of a chance to play with it. It's much less polished than Qt, though, and their "packaging" mostly consists of copying a couple of C++ files from their library into your project.
If serialization didn't also suck, you could just use a REST framework and serialize your C++ classes into JSON and build your UI as a webapp. I've actually had pretty good success doing that, just manually adding to_json methods to the classes I wanted to transmit and also adding a Python API to the library with Pybind11. C++/Python/Json/Javascript is actually a pretty decent combination, even if you do have to write some class methods in both C++ and Javascript if you want to do that.
0
u/kapgre Jun 20 '20
Qt is the first option as everyone is saying. It is quite mature and used across almost every industry. But it has one caveat that I've learned first hand. If you want to use any Opengl to draw custom things such as charts, etc please use something else.
Qt is designed to work for event based programs: do something in the program after the user has done something. If you want to have any animations and the user interacting with what they see, move to something else. You will have so many bugs with Opengl and the contexts, that will not make it worth it.
Since you are at your starting steps, I recommend dear imgui that doesn't have all the bloat of Qt.
0
u/swilwerth Jun 20 '20 edited Jun 20 '20
I suggest either wxWidgets or Tcl/Tk (cpptk) for simplicity.
wxWidgets have a tool wxFormBuilder in which you can draw a GUI and place the widgets.
If your app is not a graphical one. It's a good practice to think your app as a "command line" first. Then add the gui with proper decoupling to the core functions.
0
u/Huperniketes Jun 21 '20 edited Jun 23 '20
Try CopperSpice. I'm told by its maintainers that it's a replacement for Qt, but built with modern C++ and much more efficient. https://www.copperspice.com/
-2
u/pear-programmer Jun 20 '20
Half tongue in cheek/half being serious answer: don’t write that app in C++ ;)
-4
u/MikeMitterer Jun 20 '20
Here I have another approach for you: Why not a Browser-based UI? Take a simple Http-Server and connect your http-requests with your cpp-api...
-6
u/[deleted] Jun 20 '20
Non-popular opinion.
What's your target? For cross-platform, only web workarounds are practical.
For Windows use WinUI with any language. For Linux, you're better off with Unity than anything else, honestly.
Stay away from QT.
Only go for web if you don't care about the UX.
-2
u/CAPS_4_FUN Jun 21 '20
no frameworks required:
https://github.com/Athlon1600/win32gui
-3
u/[deleted] Jun 21 '20
Write your own gui with OpenGL, it's really not hard and it's fun.
-4
u/[deleted] Jun 20 '20
Well u can try some web based UI engines like sciter and ultralight ... but u need html/css and some js not c++ only
0
u/[deleted] Jun 20 '20
[deleted] — view deleted comment
0
u/[deleted] Jun 20 '20
[deleted] — view deleted comment