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
-
PipeWire 1.0 Officially Released
PipeWire was created to take the place of the oft-troubled PulseAudio and has finally reached the 1.0 status as a major update with plenty of improvements and the usual bug fixes.
-
Rocky Linux 9.3 Available for Download
The latest version of the RHEL alternative is now available and brings back cloud and container images for ppc64le along with plenty of new features and fixes.
-
Ubuntu Budgie Shifts How to Tackle Wayland
Ubuntu Budgie has yet to make the switch to Wayland but with a change in approaches, they're finally on track to making it happen.
-
TUXEDO's New Ultraportable Linux Workstation Released
The TUXEDO Pulse 14 blends portability with power, thanks to the AMD Ryzen 7 7840HS CPU.
-
AlmaLinux Will No Longer Be "Just Another RHEL Clone"
With the release of AlmaLinux 9.3, the distribution will be built entirely from upstream sources.
-
elementary OS 8 Has a Big Surprise in Store
When elementary OS 8 finally arrives, it will not only be based on Ubuntu 24.04 but it will also default to Wayland for better performance and security.
-
OpenELA Releases Enterprise Linux Source Code
With Red Hat restricting the source for RHEL, it was only a matter of time before those who depended on that source struck out on their own.
-
StripedFly Malware Hiding in Plain Sight as a Cryptocurrency Miner
A rather deceptive piece of malware has infected 1 million Windows and Linux hosts since 2017.
-
Experimental Wayland Support Planned for Linux Mint 21.3
As with most Linux distributions, the migration to Wayland is in full force. While some distributions have already made the move, Linux Mint has been a bit slower to do so.
-
Window Maker Live 0.96.0-0 Released
If you're a fan of the Window Maker window manager, there's a new official release of the Linux distribution that champions the old-school user interface.