Kirigami met Rust
Kirigami installeren
Alvorens te beginnen moeten we Kirigami en Rust op onze machine installeren.
sudo pacman -S cargo cmake extra-cmake-modules kirigami breeze qqc2-desktop-style | |
sudo zypper install cargo cmake kf6-extra-cmake-modules kf6-kirigami-devel qt6-quickcontrols2-devel kf6-qqc2-desktop-style | |
sudo dnf install cargo cmake extra-cmake-modules kf6-kirigami2-devel kf6-qqc2-desktop-style |
Verdere informatie voor andere distributies is hier te vinden.
Projectstructuur
Eerst maken we onze projectmap aan (u kunt de onderstaande commando's gebruiken). De onze gaan we kirigami_rust/
noemen. Dit zal de structuur van het project worden:
kirigami_rust/
├── CMakeLists.txt
├── Cargo.toml
├── build.rs
├── org.kde.kirigami_rust.desktop
└── src/
├── main.rs
└── qml/
└── Main.qml
Dit project zal CMake gebruiken om Cargo aan te roepen, die op zijn beurt het project zal bouwen.
over CMake en Cargo
Dit is geen traditionele manier van het bouwen van een Rust-project: technisch is alleen Cargo nodig om het te bouwen, gewoonlijk met cargo build
en cargo run
.
Voor bureaubladtoepassingen echter wordt CMake (of een gelijkwaardige zoals Meson gebruikt door GNOME of is Just gebruikt door COSMIC) nodig om te installeren omdat Cargo de noodzakelijke functies mist om GUI bureaubladtoepassingen te installeren.
Het project zal kirigami_rust
worden genoemd en het zal een uitvoerbaar bestand genereren genaamd kirigami_hello
.
💡 Tip
U kunt snel deze mappenstructuur aanmaken met:mkdir -p kirigami_rust/src/qml/
.org.kde.kirigami_rust.desktop
Het hoofddoel van Desktop Entry bestanden is te zorgen dat uw programma in de toepassingenstarter van Linux verschijnt. Een andere reden om ze te hebben is om bij Wayland venster-pictogrammen te hebben, omdat ze nodig zijn om de compositor te vertellen "dit venster hoort bij dit icon".
Het moet een reverse-DNS naam-schema volgen gevolgd door de .desktop
extensie zoals org.kde.kirigami_rust.desktop
:
|
|
CMakeLists.txt
The CMakeLists.txt
file is going to be used to run Cargo and to install the necessary files together with our application. It also provides certain quality of life features, such as making sure that Kirigami is installed during compilation and to signal Linux distributions to install the necessary dependencies with the application.
|
|
The first thing we do is add KDE's Extra CMake Modules (ECM) to our project so we can use ecm_find_qml_module to check that Kirigami is installed when trying to build the application, and if it's not, fail immediately. Another useful ECM feature is ECMUninstallTarget, which allows to easily uninstall the application with CMake if desired.
We also use CMake's find_package() to make sure we have qqc2-desktop-style, KDE's QML style for the desktop. This is one of the two reasons we use CMake in this tutorial.
Typically Rust projects are built with Cargo, and it won't be different here. We create a target that will simply run Cargo when run, and mark it with ALL
so it builds by default. Cargo will build the executable inside CMake's binary directory (typically build/
).
For more information about CMake, targets, and the binary directory, see Building KDE software manually.
After this, we simply install the kirigami_rust
executable generated by Cargo in the binary directory and install it to the BINDIR
, which is usually /usr/bin
, /usr/local/bin
or ~/.local/bin
. We also install the necessary desktop file to APPDIR
, which is usually /usr/share/applications
or ~/.local/share/applications
. This is the second reason we use CMake in this tutorial.
For more information about where KDE software is installed, see Building KDE software manually: The install step.
Nu we met CMake klaar zijn, laten we kijken naar de bestanden waarmee we de meeste tijd aan gaan werken.
Cargo.toml
Next we have a very straightforward Cargo.toml
:
|
|
It consists of project metadata and a list of dependencies that will be pulled automatically by Cargo, namely cxx
and cxx-qt
, which are necessary to run Qt applications written in Rust.
build.rs
Where in C++ you'd typically register QML elements with QML_ELEMENT and ecm_add_qml_module using declarative registration, with Rust you'll need to declare it in a build script build.rs file:
|
|
This is necessary to make the QML file available in the entrypoint for our application, main.rs
.
src/main.rs
The file kirigami_rust/src/main.rs
initializes the project and then loads the QML file, which will consist of the user interface for the application.
|
|
The first part that is marked with the #[cxx_qt::bridge] Rust macro just creates a dummy QObject out of a dummy Rust struct. This is needed just to complete the use of QmlModule in the previous build script build.rs
. This will play a larger part in a future tutorial teaching how to expose Rust code to QML, but for now you can ignore it.
After this starts the important part:
Lines 12-13 import the needed Qt libraries exposed through cxx-qt.
We first create a new instance of a QApplication
, then perform a few integrations in lines 20-26.
Then comes the part that actually creates the application window:
|
|
The long URL qrc:/qt/qml/org/kde/tutorial/src/qml/Main.qml
corresponds to the Main.qml
file according to the Qt Resource System, and it follows this scheme: <resource_prefix><import_URI><QML_dir><file>
.
In other words: the default resource prefix qrc:/qt/qml/
+ the import URI org/kde/tutorial
(set in build.rs
, separated by slashes instead of dots) + the QML dir src/qml/
+ the QML file Main.qml
.
src/qml/Main.qml
|
|
Hier gaan we onze frontend van de toepassing behandelen.
Als u enige Javascript kent, dan zal veel van QML u bekend voorkomen (hoewel het zijn eigen eigenaardigheden heeft). Qt's documentation heeft een uitgebreide hoeveelheid materiaal over deze taal als u zelf iets wilt proberen. Gedurende de loop van deze inleidingen zullen we veel van onze aandacht focussen op onze QML code, waar we Kirigami kunnen gebruiken om er het meeste uit te krijgen.
Laten we ons voor nu focussen op Main.qml
. Eerst importeren we een aantal belangrijke modulen:
- QtQuick, de standaard bibliotheek gebruikt in QML toepassingen.
- QtQuick Controls, die een aantal standaard besturingen levert die we kunnen gebruiken om onze toepassingen interactief te maken.
- QtQuick Layouts, die hulpmiddelen voor het plaatsen van componenten in de toepassingsvensters levert.
- Kirigami, die een aantal componenten levert geschikt voor het aanmaken van toepassingen die werken over apparaten met verschillende vormen en groottes.
Notitie
De QtQuick Controls en Kirigami imports in aparte naamruimten stoppen met het gebruik van sleutelwoordas
is een beste praktijk die verzekert dat er geen componenten zijn met dezelfde naam die in conflict kunnen zijn. U zou verschillende namen voor QtQuick Controls in het wild kunnen zien, zoals "QQC" of "QQC2". We zullen "Controls" in deze handleiding gebruiken voor alle helderheid.Daarna komen we bij ons basiselement, Kirigami.ApplicationWindow die enige basis functies levert nodig voor alle Kirigami toepassen. Dit is het venster dat elk van onze pagina's bevat, de hoofdsecties van onze UI.
Daarna zetten we de eigenschap id
van het venster op 'root'. ID's zijn nuttig omdat ze ons uniek verwijzen naar een component, zelfs als we er verschillende hebben van hetzelfde type.
We zetten de titel
van het venstereigenschap op "Hello World".
Daarna zetten we de eerste pagina van onze pagina stapel. De meeste Kirigami toepassingen zijn georganiseerd als een stapel pagina's, elke pagina bevat gerelateerde componenten passend bij een specifieke taak. Voor nu houden we het eenvoudig en blijven bij een enkele pagina. pageStack is een initieel lege stapel pagina's geleverd door Kirigami.ApplicationWindow en met pageStack.initialPage: Kirigami.Page {...}
zetten we de eerste pagina op gepresenteerd bij het laden van de toepassing op een Kirigami.Page. Deze pagina zal al onze inhoud zal bevatten.
Tenslotte voegen we in onze pagina een Controls.Label in die ons tekst laat plaatsen op onze pagina. We gebruiken anchors.centerIn: parent
om ons label horizontaal en verticaal te centreren in ons ouderelement. In dit geval is de oudercomponent van Kirigami.Page. Het laatste ding dat we moeten doen is het zetten van zijn tekst: text: "Hello World!"
.
De toepassing compileren en installeren
You should find the generated executable kirigami_hello
under build/debug/kirigami_hello
and you may run it directly or with cargo run
, but it will lack a Window icon. To address this, we'll install the application first.
Voer het volgende uit:
cmake -B build --install-prefix ~/.local
cmake --build build/
cmake --install build/
With the first command, CMake will search for Kirigami and qqc2-desktop-style.
With the second command, CMake will build the kirigami_rust
target, which just calls cargo build --target-dir build/
. This step will take a while to complete, but the next time you repeat the second CMake command it will be faster or you will not need to compile at all.
In the third step, CMake will install the executable kirigami_hello
under ~/.local/bin/kirigami_hello
and the desktop file under ~/.local/share/applications
, and a new entry named "Kirigami Tutorial in Rust" will appear on your menu.
Open het menu-item en voilà! Nu ziet u uw allereerste Kirigami toepassing voor uw eigen ogen verschijnen.
Om het nieuwe QML-programma in de modus mobiel op te starten, kan u QT_QUICK_CONTROLS_MOBILE=1
gebruiken:
QT_QUICK_CONTROLS_MOBILE=1 kirigami_hello
Als u voor een reden het project handmatig met CMake heeft gecompileerd en voor de een of andere reden het project wilt dëinstalleren, dan kan u het volgende uitvoeren:
cmake --build build/ --target uninstall