简体   繁体   中英

How to retrieve the value of a QML property from C++?

I have the following QQuickItem defined in main.qml .

Flickable {
  id: my_quick
  Accessible.name: "my_quick_item_name"
  objectName: "myquickitem"
  enabled: true

  property real quickProperty: 1.0
}

I get my_quick object in the following way on C++ side .

QQuickItem * my_quick_ptr = QmlEngine_Ptr->rootObjects()[0]->findChild<QQuickItem*>("myquickitem");

How can I get the current value of quickProperty set, into C++ side using my_quick_ptr ?

If you mean QML properties, you can use this approach:

QQmlProperty::read(my_quick_ptr, "quickProperty").toReal()

Using QObject 's facilities should also work for QML properties:

my_quick_ptr->property("quickProperty").toReal()

Also, findChild returns a QObject , so you will need to do a safe cast to get a derived pointer from it:

QQuickItem * my_quick_ptr = qobject_cast<QQuickItem *>(QmlEngine_Ptr->rootObjects()[0]->findChild<QQuickItem*>("myquickitem"));
if (my_quick_ptr) // successfully found and cast, can be safely used

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM