class TestClass : public QObject { Q_OBJECT public: explicit TestClass(QObject *parent = 0); signals: public slots: }; class TestClass : public QObject { Q_OBJECT Q_PROPERTY(int someProperty READ getSomeProperty WRITE setSomeProperty NOTIFY somePropertyChanged) public: explicit TestClass(QObject *parent = 0); int getSomeProperty()const; void setSomeProperty(const int &); private: int someProperty; signals: void somePropertyChanged(); public slots: }; int TestClass::getSomeProperty()const { qDebug() << "I'm getter"; return someProperty; } void TestClass::setSomeProperty(const int &i) { qDebug() << "I'm setter"; someProperty = i; } qmlRegisterType<TestClass>("ModuleName", 1, 0, "TypeName"); import QtQuick 1.0 import ModuleName 1.0 Rectangle { width: 360 height: 360 TypeName{ id: myObj someProperty: 10 } Text { text: "My property is: " + myObj.someProperty anchors.centerIn: parent } MouseArea { anchors.fill: parent onClicked: { Qt.quit(); } } } import ModuleName 1.0 - so we say QML engine in which module we will look for our type. TypeName{ id: myObj someProperty: 10 } text: "My property is: " + myObj.someProperty - Reading our property.
class TestClass : public QObject { Q_OBJECT Q_PROPERTY(int someProperty READ getSomeProperty WRITE setSomeProperty NOTIFY somePropertyChanged) public: explicit TestClass(QObject *parent = 0); int getSomeProperty()const; void setSomeProperty(const int &); Q_INVOKABLE void myMethod(); private: int someProperty; signals: void somePropertyChanged(); public slots: void mySlot(); }; void TestClass::myMethod() { qDebug() << "I am Method"; someProperty++; } void TestClass::mySlot() { qDebug() << "I am SLOT"; someProperty--; } import QtQuick 1.0 import ModuleName 1.0 Rectangle { width: 360 height: 360 TypeName{ id: myObj someProperty: 10 } Text { text: "My property is: " + myObj.someProperty anchors.centerIn: parent } Rectangle{ width: 20 height: 20 color: "red" MouseArea { anchors.fill: parent onClicked: { myObj.mySlot(); } } } Rectangle{ anchors.right: parent.right width: 20 height: 20 color: "blue" MouseArea { anchors.fill: parent onClicked: { myObj.myMethod(); } } } } void TestClass::myMethod() { qDebug() << "I am Method"; someProperty++; emit somePropertyChanged(); } void TestClass::mySlot() { qDebug() << "I am SLOT"; someProperty--; emit somePropertyChanged(); } void TestClass::mySlot() { qDebug() << "I am SLOT"; someProperty--; emit somePropertyChanged(); if(someProperty < 0) emit someSignal(); } TypeName{ id: myObj someProperty: 10 onSomeSignal: { Qt.quit(); } } Source: https://habr.com/ru/post/140899/
All Articles