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
-
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.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.