Important: Please read the Qt Code of Conduct -

How to get to the QML property (of the file loaded from another QML) from C++ code

  • Hi, in my application I had just one window so far and I used this construct to get to the QML properties from C++ code:

    QObject* rootObject = engine.rootObjects().first();
    QObject* textObject = rootObject->findChild<QObject*>(textName);   
    textObject->setProperty("text", str);

    Now I added one more window and need to use some screen navigation. I decided to use what TheBootroo suggested in this thread:

    Well the construct above of course ceased to work as the root has changed. Now I have no idea how to get those objects again...

    Any ideas?

  • Moderators

    Your objects should be children of Repeater's model, if I recall correctly.

  • @sierdzio Sorry, what objects? You mean the QML objects? Now the main windows looks like this:

    Window {
        objectName: "mainContentWindow"
        visible: true
        id: windowPage
        width: 1600; height: 900
        color: "black"
        // Put the name of the QML files containing your pages (without the '.qml')
        property variant pagesList  : [
        // Set this property to another file name to change page
        property string  currentPage : "main";
        Repeater {
            model: pagesList;
            delegate: Loader {
                active: false;
                asynchronous: true;
                anchors.fill: parent;
                visible: (currentPage === modelData);
                source: "%1.qml".arg(modelData)
                onVisibleChanged:      { loadIfNotLoaded(); }
                Component.onCompleted: { loadIfNotLoaded(); }
                function loadIfNotLoaded () {
                    // to load the file at first show
                    if (visible && !active) {
                        active = true;

    The SettingsMenu.qml and main.qml contains one Ractangle which contains other items such as Text elements and so on (those are the objects I need to access from the C++ side). What object should be child of the repeater model?

  • Moderators

    You've misunderstood what I meant by 'should'. I've done some research. here is what the documentation says:

    Items instantiated by the Repeater are inserted, in order, as children of the Repeater's parent.

    So your objects (main and settingsMenu) should be accessible as children of Window (which I think is not a valid QML object, so you may need to create a root rectangle for it to work).

    If you don't find them there, loop through all rootObjects(). They will definitely be somewhere.

  • @sierdzio Hm, I see. So you suggest that I should insert a Rectangle within the Window object and inside this Rectangle place the Repeater? Debugging the code - currently the there is only one root object and it is the Window. This window has but one child and this child has no name so I don't even know what that is... Might be the repeater.

  • I inserted the rectangle within the window but it still doesn't work. findChild<QObject*> still doesn't find anything (NULL pointer is returned). Debugging the code:

    • There is one root object, which is the window.
    • The root object has one child which is the newly created rectangle (Repeater is now child of this Rectangle)
    • The rectangle has just one child which is the Repeater.

    If what You say is true, the items created by the repeater should now children of the Rectangle. But as stated above, The Rectangle has only one childed which is the Repeater. Could it be somehow that the whole thing has something to do with how the repeater instantiates the pages (in the original StackOverflow post there is something about lazy instantiation)?


  • Moderators

    I doubt lazy initialization has to do anything with it - the Loader elements should be loaded in the memory, only their content may not be loaded when invisible.

  • @sierdzio Well, if the content is not loaded, how would it know what objects are in there (and thus what children to add)?

    If I change the currentPage property from the C++ code, the relevant window content is loaded, so the Repeater itself seems to work. But I still cannot access the elements of any page.

Log in to reply