官术网_书友最值得收藏!

How to do it…

We want to create a label in QML and change its text occasionally. In order to expose the label object to C++, we can do the following steps:

  1. Create a C++ class called MyLabel that extends from the QObject class in mylabel.h:
class MyLabel : public QObject {
Q_OBJECT
public:
// Object pointer
QObject* myObject;
explicit MyLabel(QObject *parent = 0);

// Must call Q_INVOKABLE so that this function can be used in QML
Q_INVOKABLE void SetMyObject(QObject* obj);
}
  1. In the mylabel.cpp source file, define a function called SetMyObject() to save the object pointer. This function will later be called in QML in mylabel.cpp:

void MyLabel::SetMyObject(QObject* obj) {
// Set the object pointer
myObject = obj;
}
  1. In main.cpp, include the MyLabel header and register it to the QML engine using the
    qmlRegisterType() function:
include "mylabel.h"
int main(int argc, char *argv[]) {
// Register your class to QML
qmlRegisterType<MyLabel>("MyLabelLib", 1, 0, "MyLabel");
}
  1. Notice that there are four parameters you need to declare in qmlRegisterType(). Besides declaring your class name (MyLabel), you also need to declare your library name (MyLabelLib) and its version (1.0), which will be used to import your class to QML.
  2. Map the QML engine to our label object in QML and import the class library we defined earlier in step 3 by calling import MyLabelLib 1.0 in our QML file. Notice that the library name and its version number have to match the one you declared in main.cpp, otherwise it will throw an error. After declaring MyLabel in QML and setting its ID as mylabels, call mylabel.SetMyObject(myLabel) to expose its pointer to C/C++ right after the label is initialized:
import MyLabelLib 1.0
ApplicationWindow {
id: mainWindow
width: 480
height: 640
MyLabel {
id: mylabel
}
Label {
id: helloWorldLabel
text: qsTr("Hello World!")
Component.onCompleted: {
mylabel.SetMyObject(hellowWorldLabel);
}
}
}
  1. Wait until the label is fully initiated before exposing its pointer to C/C++, otherwise you may cause the program to crash. To make sure it's fully initiated, call the SetMyObject() function within Component.onCompleted and not in any other functions or event callbacks. Now that the QML label has been exposed to C/C++, we can change any of its properties by calling the setProperty() function. For instance, we can set its visibility to true and change its text to Bye bye world!:
// QVariant automatically detects your data type
myObject->setProperty("visible", QVariant(true));
myObject->setProperty("text", QVariant("Bye bye world!"));
  1. Besides changing the properties, we can also call its functions by calling the following:
QMetaObject::invokeMethod():
QVariant returnedValue;
QVariant message = "Hello world!";
QMetaObject::invokeMethod(myObject, "myQMLFunction", Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, message));
qDebug() << "QML function returned:" << returnedValue.toString();
  1. Or, simply, we can call the invokedMethod() function with only two parameters if we do not expect any values to be returned from it:
QMetaObject::invokeMethod(myObject, "myQMLFunction");

主站蜘蛛池模板: 宁津县| 南投市| 会昌县| 三亚市| 来凤县| 吉安市| 尤溪县| 房产| 日照市| 九江县| 新巴尔虎右旗| 南漳县| 阳谷县| 云浮市| 仲巴县| 泗洪县| 砀山县| 安乡县| 永康市| 蓝山县| 通化县| 内黄县| 乌兰察布市| 应城市| 苗栗市| 云梦县| 太湖县| 潮安县| 花垣县| 卢氏县| 报价| 西畴县| 南京市| 增城市| 苍梧县| 舞阳县| 苍梧县| 马鞍山市| 渝北区| 佛山市| 长葛市|