3
I’m trying to change a property of an object (Rectangle) that was instantiated in the QML, but I am not able to perform it because the method findChild returns null.
I am following the following documentations:
-Interacting with QML Objects from C++
Main
#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine(QUrl("qrc:/main.qml"));
QQmlContext *ctxt = engine.rootContext();
QObject *rect = ctxt->findChild<QObject*>("rect");
if (rect)
rect->setProperty("color", "blue");
return app.exec();
}
QML
ApplicationWindow {
id: app
title: "Redi"
width: 640
height: 480
visible: true
visibility : "Maximized"
Rectangle{
y:100
x:100
width: 100
height: 100
objectName: "rect"
}
}