- Qt5 C++ GUI Programming Cookbook
- Lee Zhi Eng
- 470字
- 2021-07-02 12:17:56
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:
- 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);
}
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;
}
- 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");
}
- 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.
- 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);
}
}
}
- 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!"));
- 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();
- 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");
推薦閱讀
- 實(shí)用口腔正畸臨床技術(shù)圖譜
- 揭秘卒中后非運(yùn)動(dòng)障礙:失眠 抑郁 認(rèn)知障礙
- 新編男性不育與不孕
- 腎病綜合征
- 醫(yī)學(xué)統(tǒng)計(jì)學(xué)實(shí)戰(zhàn)指導(dǎo)
- 眼科手術(shù)操作與技巧(第2版)
- 世界衛(wèi)生組織成人和青少年癌痛藥物治療和放射治療管理指南
- 高脂血癥百家百方
- 膜性概念神經(jīng)外科學(xué)
- 孤獨(dú)癥譜系障礙兒童家庭訓(xùn)練100問
- 臨床急癥介入治療學(xué)
- 神經(jīng)系統(tǒng)疾病的檢驗(yàn)診斷(第2版)
- Learning Geospatial Analysis with Python
- 原發(fā)性肝癌放射治療臨床實(shí)踐
- 中國(guó)膠質(zhì)瘤放射治療專家共識(shí)