17

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?

yakobom
  • 2,681
  • 1
  • 25
  • 33
HarvyD
  • 203
  • 1
  • 2
  • 5
  • https://stackoverflow.com/questions/17640887/qt-ui-with-c-vs-with-xml-vs-with-qml/17640989#17640989 – Mohammad Kanan May 02 '18 at 18:38
  • Possible duplicate of [Qt: UI with c++ vs with xml vs with qml](https://stackoverflow.com/questions/17640887/qt-ui-with-c-vs-with-xml-vs-with-qml) – sandwood May 04 '18 at 07:16

1 Answers1

33

UI Technology

It's actually not so much a question of QML vs C++, rather it is a question of what UI technology to use with Qt:

  • QtWidgets (code written with C++)
  • QtQuick (code written with QML/JS)
  • HTML5 (via WebEngine, embedded into a widget or QtQuick item)
  • OpenGL (embedded into a widget or QtQuick item)

Leaving aside HTML5 and OpenGL, the question QtWidgets vs QtQuick has been discussed in other placed, for example at Qt Quick vs. Qt Widget and at this Qt DevDays 2014 presentation.

My personal opinion: Use QtWidgets for traditional desktop applications, and QtQuick for mobile and embedded (devices with touchscreen), unless you have good reasons to do otherwise. QtWidgets has better and more mature support for traditional desktop controls, while QtQuick is better for animations and completely custom styling.

One reason to use QtQuick on the desktop is when you want lots of custom animations and styling, at the cost of fighting a bit more with traditional desktop controls such a toolbars, menubars, drag&drop etc.

Language

When choosing QtWidgets, the language is always C++ (well, unless you use the Python bindings). While you can use the Qt Designer tool to visually create UIs, in the end those get compiled to C++.

When choosing QtQuick, the language for the UI parts will be QML and JavaScript. However, in any moderately large QtQuick application, you will at some point also have a C++ part - for example to interface with other C and C++ libraries, for operations that don't have an associated JavaScript API or simply for quicker and more maintainable code than JS. C++ classes and objects can be accessed from QML, check the documentation for more details.

Thomas McGuire
  • 5,308
  • 26
  • 45
  • Good summary of the two techs! One thing I always wondered about is performance. Is an interface created with QML slower or faster or equal in speed to the same thing created with QtWidgets (+qss, maybe)? – TheSHEEEP May 03 '18 at 06:41
  • 9
    @TheSHEEEP The rendering speed in QtQuick is, at least for simple cases, faster than with QtWidgets, as the rendering with QtQuick is done on the GPU with OpenGL, while the rendering with QtWidgets is done on the CPU. However, in QtQuick/QML, loading a QML page usually takes longer than the equivalent QtWidgets C++ code, and can become a bottleneck if not careful. – Thomas McGuire May 03 '18 at 16:29
  • Qt5 is deprecated? @ThomasMcGuire – PersianGulf Oct 22 '19 at 05:29
  • @PersianGulf Qt5 is not deprecated, as Qt6 hasn't been released yet. – Thomas McGuire Oct 30 '19 at 11:25