How to pass an object ref from C++ to QML?
-
Hello everyone,
I got wondered about how to use some QML properties from C++.
For example, I have got the pointer "pItem" of an QML item by "mainWindow->findChild()",
and I'd like to set the "anchors.fill" property. So, I tried:``` pItem->setProperty("anchors.fill", mainWindow); pItem->setProperty("anchors.fill", *mainWindow); ```
All cannot work.
Dose anyone know how to do it?
Thanks. -
Hello everyone,
I got wondered about how to use some QML properties from C++.
For example, I have got the pointer "pItem" of an QML item by "mainWindow->findChild()",
and I'd like to set the "anchors.fill" property. So, I tried:``` pItem->setProperty("anchors.fill", mainWindow); pItem->setProperty("anchors.fill", *mainWindow); ```
All cannot work.
Dose anyone know how to do it?
Thanks.First, don't do that (interact with QML objects from C++).
Here are some links explaining why:- https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
- http://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
- https://youtu.be/vzs5VPTf4QQ?t=23m20s
As for why your code doesn't work,
anchors.fill
expects aQQuickItem*
, which I doubt yourmainWindow
is. -
First, don't do that (interact with QML objects from C++).
Here are some links explaining why:- https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
- http://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
- https://youtu.be/vzs5VPTf4QQ?t=23m20s
As for why your code doesn't work,
anchors.fill
expects aQQuickItem*
, which I doubt yourmainWindow
is.Thanks, @GrecKo
I found some persons have the same problem with me, they also want to control the anchors properties from C++.
I continue to try, and finally found how to do it, here is the code:auto anchors = pItem->property("anchors").value<QObject*>(); auto b = anchors->setProperty("fill", QVariant::fromValue(m_qtRootItem));
-
-
-