Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to get to the QML property (of the file loaded from another QML) from C++ code
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QML and Qt Quick
qt quick
8 Posts 2 Posters 3.5k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aros
    wrote on last edited by Aros
    #1

    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: http://stackoverflow.com/questions/13049896/qml-navigation-between-qml-pages-from-design-perception

    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?
    thanks

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

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

      (Z(:^

      A 1 Reply Last reply
      0
      • sierdzioS sierdzio

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

        A Offline
        A Offline
        Aros
        wrote on last edited by Aros
        #3

        @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  : [
                "main",
                "settingsMenu"
            ];
        
            // 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?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          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.

          (Z(:^

          A 1 Reply Last reply
          0
          • sierdzioS sierdzio

            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.

            A Offline
            A Offline
            Aros
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Aros
              wrote on last edited by
              #6

              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)?

              Thanks

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                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.

                (Z(:^

                A 1 Reply Last reply
                0
                • sierdzioS sierdzio

                  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.

                  A Offline
                  A Offline
                  Aros
                  wrote on last edited by
                  #8

                  @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.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved