各位大佬,
C++读写
qml属性
问题已经困扰我几天了,我按官网上的方法(https://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html),用QQmlComponent创建对像,用findChild查找到对象后,用setProperty设置属性的值,如设置Label的text,qml文件中Label的onTextChanged已经触发了,打印text也已经改变了,但ui的
显示不更新是什么原因呢?如果我在C++中读Label的text值一直读到的是初始值,text改变了读到的值不会变。这是什么原因啊
如果我想在C++中设置或获得一个Label控件的text值,该怎么做呢。
- mainwindow::mainwindow(QObject *parent) : QObject(parent)
- {
- QQmlEngine *page2_engine = new QQmlEngine;
- QQmlComponent *component = new QQmlComponent(page2_engine, "qrc:/Page2Form.ui.qml");
- page2_obj = component->create();
- led_obj = page2_obj->findChild<QObject*>("led");
- data_2_obj = page2_obj->findChild<QObject*>("data_2");
- warm1_obj = page2_obj->findChild<QObject*>("warm1");
- }
- void mainwindow::button_test()
- {
- static int index = 0;
- qDebug("button_test()");
- warm1_obj->setProperty("text", index);
- index ++;
- }
- Label {
- id: lable1
- objectName: "warm1"
- text: qsTr("预热状态:")
- onTextChanged: {
- console.log("text = ", text)
- }
- }