Konektu backend por fari kalkulojn kaj provizi vian uzantinterfacon kun datumoj por montri
To integrate logic into the application, we need C++ backend classes that can do the important calculations. Writing logic in the QML files is discouraged, so try to move as much as possible to the backend such that QML is used only for displaying the user interface.
First, create the header file that will have code exposed to QML, namely the Backend type:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#pragma once
#include<QObject>#include<qqmlintegration.h>classBackend:publicQObject{Q_OBJECTQML_ELEMENTQML_SINGLETONpublic:explicitBackend(QObject*parent=nullptr);};
There are two things needed to expose C++ code to QML, and one of them is simply using the QML_ELEMENT macro, available in the <QtQml/qqmlregistration.h> header.
The backend will be created as a singleton, which means it will only be created once and exist for the whole duration of the application lifetime. For this, we use the QML_SINGLETON macro.
src/components/backend.cpp
We can add our initial code for the constructor to backend.cpp:
globalDrawer:Kirigami.GlobalDrawer{isMenu:trueactions:[Kirigami.Action{text:i18n("Exposing to QML")icon.name:"kde"onTriggered:pageStack.push(Qt.createComponent("org.kde.tutorial.components","ExposePage"))},Kirigami.Action{text:i18n("Quit")icon.name:"application-exit-symbolic"shortcut:StandardKey.QuitonTriggered:Qt.quit()}]}
src/components/CMakeLists.txt
Lastly, add the newly created backend.h, backend.cpp and ExposePage.qml to CMake:
Nun ni konektis la klason tenantan la estontan logikon al la aplikaĵo, sed ĝi ankoraŭ nenion faras. Por ŝanĝi tion, ni aldonu proprecon al la klaso. Propraĵoj estas multe pli ol simpla variablo. Ili povas informi la UI pri ŝanĝoj, por ke ĝi povu ĝisdatigi la ĝustajn areojn.
Right under the QML_SINGLETON macro, add a new Q_PROPERTY.
Ĉi tio povas ŝajni kiel multe da kodo por simple legi kaj skribi iom da kodo de la malantaŭo. Tamen, pli proksima rigardo rivelas, ke legado de la propreco el la UI jam povas funkcii iun logikon—same kiam ĝi estas skribita al ĝi. En ĉi tiu kazo, ĝi aŭtomate informos la fasadon kaj backend pri ŝanĝoj.
La legado kaj skribo baziĝas sur la koncepto de [funkcioj de ricevilo kaj agordilo] (https://www.w3schools.com/cpp/cpp_encapsulation.asp). Antaŭeniru kaj aldonu novan privatan atributon al via klaso, kiu enhavas la datumojn, kaj ankaŭ la koncernajn ricevilo- kaj setter-funkciojn.
The first function is the getter, the second the setter, and the third a signal that is emitted when the property is changed. They match the READ, WRITE and NOTIFY parts of the above Q_PROPERTY.
The result should look like the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once
#include<QObject>#include<qqmlintegration.h>classBackend:publicQObject{Q_OBJECTQML_ELEMENTQML_SINGLETONQ_PROPERTY(QStringintroductionTextREADintroductionTextWRITEsetIntroductionTextNOTIFYintroductionTextChanged)public:explicitBackend(QObject*parent=nullptr);QStringintroductionText()const;voidsetIntroductionText(constQString&introductionText);Q_SIGNALvoidintroductionTextChanged();private:QStringm_introductionText=QStringLiteral("Hello World!");};
src/components/backend.cpp
The signal doesn't need any implementation in the backend.cpp file, since it doesn't do much more than being emitted, but the getter and setter need to be implemented similar to the following:
Kiel vi povas vidi, kiam la setter estas vokita, la signalo estos elsendita, kaj informos la ui kaj backend pri la ŝanĝo.
src/components/ExposePage.qml
To display the text, add a Kirigami.Heading to src/components/ExposePage.qml under the title property of the Kirigami.Page component we added to the code.
La rezulta kodo en tiu parto de la dosiero devus aspekti jene:
1
2
3
4
5
6
7
8
9
10
importorg.kde.kirigamiasKirigamiimportorg.kde.tutorial.componentsKirigami.Page{title:"Exposing to QML Tutorial"Kirigami.Heading{anchors.centerIn:parenttext:Backend.introductionText}}
#pragma once
#include<QObject>#include<qqmlintegration.h>classBackend:publicQObject{Q_OBJECTQML_ELEMENTQML_SINGLETONQ_PROPERTY(QStringintroductionTextREADintroductionTextWRITEsetIntroductionTextNOTIFYintroductionTextChanged)public:explicitBackend(QObject*parent=nullptr);QStringintroductionText()const;voidsetIntroductionText(constQString&introductionText);Q_SIGNALvoidintroductionTextChanged();private:QStringm_introductionText=QStringLiteral("Hello World!");};
importorg.kde.kirigamiasKirigamiimportorg.kde.tutorial.componentsKirigami.Page{title:"Exposing to QML Tutorial"Kirigami.Heading{anchors.centerIn:parenttext:Backend.introductionText}}
Now compile and start your program again. You'll see that the new page has a centered Heading saying "Hello World!".
Gratulon, vi lernis:
Kiel registri backend tipojn al QML
Aldonu novajn elementojn al la QML-dosiero
Kreu novajn QObject-subklasojn
Kiel aldoni proprietojn kaj kion ili faras
Kiaj signaloj estas
Se vi volas scii pli pri la integriĝo inter QML kaj C++, ni rekomendas legi la oficialan Qt-dokumentadon.