Ask HN: Which parts of C++ do I need to learn in order to use Qt?
I am looking to write a cross-platform GUI to sit on top of, and display the results of, some number-crunching algorithms. (The code to do the number-crunching calculations has already been written in C.) The consensus seems to be that Qt is the best toolkit for such cross-platform user interfaces.
Unfortunately, using Qt requires writing C++, a language I have very little experience with. Which parts of C++ do I need to learn in order to use Qt?
For example, it seems I will not need to learn the details of exception-safety in C++, as Qt does not use exceptions [1]. Similarly it will not be necessary to learn the details of the standard library containers, because Qt provides its own versions [2].
Are there any other parts of C++ which I can safely ignore if I only intend to use the language with Qt? Is there agreement on a subset of the language which forms "Qt-flavoured" C++?
[1] http://doc.qt.io/qt-5/exceptionsafety.html
[2] http://doc.qt.io/qt-5/containers.html
(
11 comments
| 10 points
| on HN
)
Objects; they're all objects. Understand objects.
RAII/Constructors and destructors; managing object and resource lifetimes, and suitably ensuring that visual widgets and other such Qt objects are created and disposed of properly.
Threads; Qt Widgets (and your own objects) spark actions in Qt objects through signals and slots (fancy Qt meta-programming that takes care of a lot of boilerplate for you), that have rules about working across threads. Understanding threads is needed to understand these restrictions, and the GUI rule of "stay off the GUI thread" applies always, if you don't want to lock up the GUI. This means you'll need to create other threads to do the heavy lifting. You don't have to use standard C++ threads, but you'll need to understand threading.
Don't use QList unless you really have a good reason, even if you find old documentation saying you should.
And the signals/slots do a great job of marshalling data across the threads, which is where I've found a lot of C++ projects get into the weeds.
Even in the context of Qt, it's worth using some C++11. auto, lambda functions, shared and unique pointers, (although QT has always provided its own versions of these), C++ 11 threads. I code heavily with Qt and I use these C++11 features daily. C++11 changes some things that used to be almost impossible into possible, and some things that used to be awkward maintenance headaches into natural and flowing code.
This is great; this is an opportunity for you to get to grips with some modern C++.
For a book about C++, I'll would go with C++ Primer.
- Qt by itself is a little bit weird, because it introduces its custom build process and it mainly uses a signal/slot mechanism for communication (thinks callback). Have also a look at Qt Quick, you can design an GUI without writing a single line of C++. There is a list of books about Qt there : https://wiki.qt.io/Books
Charged with the task of making a GUI, I'd start with HTML, CSS, Javascript and REST. Absent some compelling reason to pursue another course, that's where I'd end.
Good luck.
This requires you to write a class that derives from QObject but that's almost it.
Your UI logic in QML is written in JavaScript but you don't usually need much and it's pretty straightforward to pick up as you go.
https://github.com/andlabs/libui
However, I would like to build the application into a standalone binary for end-users to run on their own systems. This seems to be difficult with Python, and when I was able to build such a binary (using PyInstaller on Windows) it was large and slow to start-up.
(Qt itself is ~10MB (QtCore4.dll + QtGui4.dll) and (when using C++) can be made smaller by static linking, but PyQt doubles the size (python27.dll + PyQt4.QtCore.pyd + PyQt4.QtGui.pyd).)