Changing the ownership of a QML object by C ++

3

I'm trying to make a change to a property of a (Rectangle) object that was instantiated in QML, but I'm not able to do it because the findChild strong null .

I'm following the following documentation:

- Integrating QML and C ++

asked by anonymous 04.11.2016 / 19:04

1 answer

1

I found a solution, but I could not understand why the findChild method returns null . When I performed some tests, I realized that the rootObjects method returned an object that had the same objectname as the object that has the object that I want to change the property and thus returns the window I was able to perform the property change and search for other objects.

Basically the source stayed this way.

Main

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickView>
#include <QQuickItem>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    QObject *rect = engine.rootObjects().first()->findChild<QObject *>("rect");//Alerado de rootContext para rootObjects
    if (rect)
        rect->setProperty("color", "blue");

    return app.exec();
}

Result

In Qt Creator design mode

Intheapp

    
09.11.2016 / 18:57