Qt vs QML language relevance
-
So, I'm a newbie in Qt and my intent is to familiarize myself with Qt Graphics. However, most of the guides on the internet suggest to use QML instead Qt C++ when it comes to dealing with graphics.
Here is my question: What is the difference between Qt C++ and QML? What does QML give us, that Qt C++ does not?
-
First, I'll list more graphic-related stuff in Qt, to give you a broader picture:
- QtWidgets - standard components for creating windows, dialogs etc. Useful mostly on desktops, but work on mobile platforms, too
- QtQuick (QML) - declarative components, using a new language (QML and JavaScript). Great on mobile platforms, embedded devices etc. but work on desktops, too
- Qt3D - well, as the name implies, a 3D toolkit within Qt
- QGraphicsView - part of Qt widgets, allows building 2D scenes, painting shapes etc.
- QPainter - the base painting mechanism of QtWidgets
- QtSceneGraph - base painting mechanism of QtQuick
- QtCharts - a module useful for creating charts
Now, with that information out, let's focus on your questions:
What is the difference between Qt C++ and QML?
There are so many differences it's hard to start somewhere. C++ is an imperative language (you state, line by line, what the CPU has to do) while QML is declarative (you declare how app should behave, and QML engine figures out the rest). This is an important difference, takes some time to get used to.
To explain it in short: imagine you have a variable
temperature
and you have a thermometer which updates the reading every second. In C++, you would do something like this:connect(thermometer, &Thermal::reading, this, &App::updateValue); /// [...] void updateValue(int temp) { value = temp; // Then you probably also want to update your GUI view }
In QML, the same is achieved with:
value: thermometer.temp
QML engine knows that
value
needs to change each timetemp
changes.Further differences:
- QML is an interpreted language and uses JavaScript to evaluate expressions. C++ is compiled
- QML allows one to easily and quickly design new interfaces, especially when they involve a lot of animations, timers etc. (that's why they are recommended on mobile platforms)
- however, with QML it's a bit harder to create large architectures, it gets rather complex in the process
- with C++ you get access to a lot of additional technologies aimed at optimizing applications (valgrind, asan, perf, compiler optimizations etc.), in QML there aren't so many tools
- with QtWidgets, architectures are usually clearer and IDEs have no problem to follow symbols, find their uses etc.
Now, to make matters even more complicated: you can use C++ with QML. You can also (via external module - DeclarativeWidgets) use widgets in QML. Under the hood it's all C++ anyway ;-)
I probably confused you even more. I'd recommend trying to create some simple app in both technologies and you'll have a much clearer view of the matter.
From experience - both QML and QtWidgets are in active use, it's not like one technology is winning over the other. They are both good and useful, for slightly different use cases.