How to access a nested QML object from C++?
Here is a reproducible example:
main.qml
Code:
import QtQuick 2.0
Item {
id : root
width: 360
height: 360
Text {
id : t1
text: qsTr("Hello World")
property int someNumber: 1000
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
}
main.cpp
Code:
#include <QtGui/QGuiApplication>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QQmlProperty>
#include <QDebug>
#include "qtquick2applicationviewer.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/untitled/main.qml"));
viewer.showExpanded();
QQmlEngine engine;
QQmlComponent component(&engine, "qml/untitled/main.qml");
QObject *object
= component.
create();
qDebug() << "Property value:" << QQmlProperty::read(object, "root.ti.someNumber").toInt();
return app.exec();
}
I wish to access the property somenumber of the text of the QML Item. The above method isn't producing the desired result.
How to do it?
Re: How to access a nested QML object from C++?
QQmlProperty::read() needs the object from which to read the property. So you need to find the Text element.
You can set the objectName property on the Text element and then use QObject::findChild on the object you have.
However, I would strongly advise to reconsider doing that at all. Accessing QML generated objects from C++ like that tightly couples the C++ code to the QML code, which is usually the very opposite of what one would want.
What is the goal you are trying to reach?
Cheers,
_
Re: How to access a nested QML object from C++?
Quote:
However, I would strongly advise to reconsider doing that at all. Accessing QML generated objects from C++ like that tightly couples the C++ code to the QML code, which is usually the very opposite of what one would want.
Agreed. I think the preferred way to do this is the same way you would make a C++ class - hide the implementation details behind public methods that do what you need behind the scenes. Users of the public UI don't need to know anything about what happens inside the class.
In the QML case, you can declare functions inside your top-level Item that reach down into the QML hierarchy to get, set, or do something. Outside the QML, the user only knows that there is some function that can retrieve a number from somewhere. This enables you to loosely couple the C++ and QML. Another alternative is to use signals and slots to talk between the two layers.
Code:
import QtQuick 2.0
Item {
id : root
width: 360
height: 360
function getSomeNumber() {
return t1.someNumber
}
function setSomeNumber( int number ) {
t1.someNumber = number
}
Text {
id : t1
text: qsTr("Hello World")
property int someNumber: 1000
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
}
Re: How to access a nested QML object from C++?
Quote:
Originally Posted by
d_stranz
In the QML case, you can declare functions inside your top-level Item that reach down into the QML hierarchy to get, set, or do something. Outside the QML, the user only knows that there is some function that can retrieve a number from somewhere. This enables you to loosely couple the C++ and QML. Another alternative is to use signals and slots to talk between the two layers.
Or a propery on the C++ side that you simply bind to in QML.
Cheers,
_