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
-
Wine 10 Includes Plenty to Excite Users
With its latest release, Wine has the usual crop of bug fixes and improvements, along with some exciting new features.
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.