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
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.
-
Plasma Desktop Will Soon Ask for Donations
The next iteration of Plasma has reached the soft feature freeze for the 6.2 version and includes a feature that could be divisive.
-
Linux Market Share Hits New High
For the first time, the Linux market share has reached a new high for desktops, and the trend looks like it will continue.
-
LibreOffice 24.8 Delivers New Features
LibreOffice is often considered the de facto standard office suite for the Linux operating system.
-
Deepin 23 Offers Wayland Support and New AI Tool
Deepin has been considered one of the most beautiful desktop operating systems for a long time and the arrival of version 23 has bolstered that reputation.
-
CachyOS Adds Support for System76's COSMIC Desktop
The August 2024 release of CachyOS includes support for the COSMIC desktop as well as some important bits for video.
-
Linux Foundation Adopts OMI to Foster Ethical LLMs
The Open Model Initiative hopes to create community LLMs that rival proprietary models but avoid restrictive licensing that limits usage.
-
Ubuntu 24.10 to Include the Latest Linux Kernel
Ubuntu users have grown accustomed to their favorite distribution shipping with a kernel that's not quite as up-to-date as other distros but that changes with 24.10.
-
Plasma Desktop 6.1.4 Release Includes Improvements and Bug Fixes
The latest release from the KDE team improves the KWin window and composite managers and plenty of fixes.
-
Manjaro Team Tests Immutable Version of its Arch-Based Distribution
If you're a fan of immutable operating systems, you'll be thrilled to know that the Manjaro team is working on an immutable spin that is now available for testing.