0

I am a newbie in QML, and cannot resolve a simple issue. I want to get access to the QML components from C++, but I cannot. The pointer test is always 0. What can be the reason?

The code is the following:

main.cpp

int main(int argc, char *argv[])
{
    QGuiApplication &app=reg6::Bonder::BonderGuiApplication::instance();
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    QObject* test=engine.rootObjects().first()->findChild<QObject*> ("cameraArea");
    test->setProperty("color","black");

    return app.exec();
}

main.qml

import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.0

ApplicationWindow {
visible: true
width: 1800
height: 900

SplitView
{
    anchors.fill: parent
    orientation: Qt.Vertical
    SplitView {
        Layout.fillHeight: true
        SplitView {
            orientation: Qt.Vertical
            width:400
            Layout.minimumWidth: 400
            Layout.maximumWidth: 500

            Camera {
                id: cameraArea
                height: 400
                Layout.maximumHeight: 400
                Layout.minimumHeight: 300
            }
            List {
                id: listArea
            }
        }

        Bonder {
            id: mainArea
            Layout.fillWidth: true
        }

        Properties {
            id: propertiesArea
            Layout.minimumWidth: 300
            Layout.maximumWidth: 400
        }
    }
    Error {
        id: errorArea
        Layout.minimumHeight: 100
        height: 200
    }
}
}

Camera.qml

import QtQuick 2.5
Rectangle {
    color: "lightblue"
}

1 Answer 1

1

You have to set the objectName property also of the QML component to get a valid pointer to your QObject because T QObject::findChild(const QString &name = QString(), Qt::FindChildOptions options = Qt::FindChildrenRecursively) const needs the objectName not the ID

Sign up to request clarification or add additional context in comments.

5 Comments

I think an answer to such question can't be complete without the Warning from the documentation. It is just the paragraph above the linked headline here. It starts with: Warning: While it is possible to use C++ to access and manipulate QML objects deep into the object tree, we recommend that you do not take this approach outside of application testing and prototyping.[...]
@derM Indeed, the other way around is much better.
@derM You are right but it doesn't answer the question Lecko asked so the answer for now is complete.
@Redanium: You are right with that it is not an answer, but especially as Lecko states, he is a newbie in QML, I think it is important to go further than to just simply answer. It is just the same in the documentation: It would not be needed, but it is helpful! Otherwise anyone who reads this, starts with it, runs into problems, asks here, and all the answers will be just workarounds (as this is what is being asked for), and things won't get better and no one improves - while a simple hint at the very beginning had sufficed to prevent this unforunate chain of problems, eradicating the root
@Redanium can you please detail your answer, it is not really clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.