Use QML to build smart graphical applications


Now that your app is complete, it would be cool if you didn't have to invoke it with qmlscene every time but, instead, run it just like any other app. To do that, you need to compile it to binary, which will not only make it a standalone app but will also make it faster and more compact.

Before you start, make sure you have your basic compilation tools installed:

sudo apt install gcc g++ make

Then, you will need some Qt-specific packages:

sudo apt install qt5-qmake qtdeclarative5-dev

With the qmake tool, you will create a makefile that contains the information the compilers need to generate the binaries. The qtdeclarative5-dev package contains the libraries the compilers need to link against to generate binary code from the QML scripts.

Apart from your source code .qml files, you'll need to create three other files.

  1. qml.qrc (Listing 5) contains the QML resources needed in the project (line 3). Because a QML application can contain various files (something I will cover in future installments) and span several folders, you state here what goes into the project and where it is located.

Listing 5


01 <RCC>
02     <qresource prefix="/">
03         <file>webcam.qml</file>
04     </qresource>
05 </RCC>
  1. webcam.cpp (Listing 6) is the compiled C++ program. It is essentially a wrapper that loads several libraries and sets up an engine before calling in your .qml file (line 11).

Listing 6


01 #include <QGuiApplication>
02 #include <QQmlApplicationEngine>
04 int main(int argc, char *argv[])
05 {
06     QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
08     QGuiApplication app(argc, argv);
10     QQmlApplicationEngine engine;
11     engine.load(QUrl(QStringLiteral("qrc:/webcam.qml")));
12     if (engine.rootObjects().isEmpty())
13         return -1;
15     return app.exec();
16 }
  1. (Listing 7) is the file qmake uses to generate the makefile. It adds the generic Qt tools make needs to be aware of (lines 1 and 2), as well as the files for your project (lines 3 and 4).

Listing 7

QT += quick
CONFIG += c++11
SOURCES += webcam.cpp
RESOURCES += qml.qrc

Once you have all your files, call qmake from the command line from within the directory containing your files to generate a makefile. Next, run make to compile.

At the end of the process, you will see an executable file with the same name as your .pro file (in this case, webcam). That is your app, and you can run it from the command line or, in most graphical environments, by double-clicking it in your file manager.


Writing applications in QML is easy and, dare I say it, fun! Plus, although KDE's Plasma desktop is based on Qt, your apps will play really well with everything: Gnome, XFCE, Windows, Mac OS, and even Android. That means that the applications you write in QML will work on most desktops and, small screens allowing, on your smartphone.

In future installments, I will talk about developing for other platforms. Meanwhile, enjoy coding!

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Free Software Projects

    Voice over IP on the Internet gives communication a personal touch, but it takes applications like Cheese and WebcamStudio to exploit the creative potential of Internet telephony.

  • Webcam Rescue

    If your new webcam doesn't work with the default software on your Linux system, try your luck with Guvcview or QtCAM.

  • aMSN in Linux

    aMSN lets Linux users communicate with associates on Microsoft instant messaging networks. In this article, we'll show you how to reach out to your friends in the empire.

  • Amsn

    Many Webcam owners use MSN Messenger by Microsoft for video messaging. Linux users can run Amsn to connect eye to eye.

  • VokoscreenNG

    The VokoscreenNG screencast tool offers many options but is still surprisingly easy to use.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.