Use QML to build smart graphical applications
Compiling
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.
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
qml.qrc
01 <RCC> 02 <qresource prefix="/"> 03 <file>webcam.qml</file> 04 </qresource> 05 </RCC>
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
webcam.cpp
01 #include <QGuiApplication> 02 #include <QQmlApplicationEngine> 03 04 int main(int argc, char *argv[]) 05 { 06 QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); 07 08 QGuiApplication app(argc, argv); 09 10 QQmlApplicationEngine engine; 11 engine.load(QUrl(QStringLiteral("qrc:/webcam.qml"))); 12 if (engine.rootObjects().isEmpty()) 13 return -1; 14 15 return app.exec(); 16 }
webcam.pro
(Listing 7) is the fileqmake
uses to generate the makefile. It adds the generic Qt toolsmake
needs to be aware of (lines 1 and 2), as well as the files for your project (lines 3 and 4).
Listing 7
webcam.pro
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.
Conclusion
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!
Infos
- "Tutorials – gpsd" by Paul Brown, Linux Pro Magazine, issue 210, May 2018, pg. 90, http://www.linuxpromagazine.com/Issues/2018/210/Tutorials-gpsd
- Interactive map: https://youtu.be/jcHOY07Rvpk
- Cheese: https://wiki.gnome.org/Apps/Cheese
- Kamoso: https://userbase.kde.org/Kamoso
- QML Window type: http://doc.qt.io/qt-5/qml-qtquick-window-window.html
- QML Camera type: http://doc.qt.io/qt-5/qml-qtmultimedia-camera.html
- QML VideoOutput type: http://doc.qt.io/qt-5/qml-qtmultimedia-videooutput.html
- Positioning QML objects with anchors: http://doc.qt.io/qt-5/qtquick-positioning-anchors.html
- QML Button type: http://doc.qt.io/qt-5/qml-qtquick-controls-button.html
- Qt built-in quit() method: http://doc.qt.io/qt-5/qml-qtqml-qt.html#quit-method
- QML Connections type: http://doc.qt.io/qt-5/qml-qtqml-connections.html
- Camera videoRecorder property: http://doc.qt.io/qt-5/qml-qtmultimedia-camerarecorder.html
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.