3

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?

scopchanov
  • 7,966
  • 10
  • 40
  • 68
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159

1 Answers1

4

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
dtech
  • 47,916
  • 17
  • 112
  • 190
  • thanks, can you pls make your answer inline with the sample code in my question? I am using a `qreal` with objectName as `myquickitem` & the property name is `quickProperty` – TheWaterProgrammer Sep 01 '17 at 20:53
  • @dtech, on Qt 6.2.0 I get this warning "Don't call QList::operator[]() on temporary [clazy-detaching-temporary]" on the `qobject_cast` line. – Mark Dec 14 '21 at 12:26