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. Anchoring Items of a Component Within Loader Obeject
Forum Updated to NodeBB v4.3 + New Features

Anchoring Items of a Component Within Loader Obeject

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 3 Posters 890 Views 2 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.
  • K Offline
    K Offline
    kengineer
    wrote on last edited by kengineer
    #1

    I am trying to use a loader object to dynamically load different components I have defined in QML (within the same file). I am finding that I am not able to use anchors in the way I think that I should be able to:

    i.e. simple example, column with 3 rectangles, this works fine in my QML window, as expected, the 3 rectangles are horizontally centered in the parent window:

    Column {
        anchors.horizontalCenter: parent.horizontalCenter
        spacing : 50
    
        Rectangle {
            color: "Red"
            width: 50
            height: 50
        }
    
        Rectangle {
            color: "Red"
            width: 50
            height: 50
        }
    
        Rectangle {
            color: "Red"
            width: 50
            height: 50
        }
    }
    

    However, if I encapsulate this as a component, and use a Loader to load an instance of it, they are no longer centered, the anchor statement is ignored:

    Loader {
    id: myCoolLoader
    anchors.fill: parent
    sourceComponent: myCoolItem
    }
    
    Component {
    id: myCoolItem
    
    Column {
        anchors.horizontalCenter: parent.horizontalCenter
        spacing : 50
    
        Rectangle {
            color: "Red"
            width: 50
            height: 50
        }
    
        Rectangle {
            color: "Red"
            width: 50
            height: 50
        }
    
        Rectangle {
            color: "Red"
            width: 50
            height: 50
        }
    }
    }
    

    How can I make this work or what am I doing wrong here? Maybe since a loader is dynamic the child isn't able to reference the parent the same way? I also tried directly using id "myCoolLoader" rather than parent.

    ODБOïO 1 Reply Last reply
    0
    • Shrinidhi UpadhyayaS Offline
      Shrinidhi UpadhyayaS Offline
      Shrinidhi Upadhyaya
      wrote on last edited by
      #2

      Hi @kengineer , you need to anchor the Loader, so instead of filling it you need to anchor the loader horizontal.

      Here is the sample code:-

         Loader {
                         id: myCoolLoader
                         //anchors.fill: parent
                         anchors.horizontalCenter: parent.horizontalCenter
                         sourceComponent: myCoolItem
                     }
      

      Shrinidhi Upadhyaya.
      Upvote the answer(s) that helped you to solve the issue.

      1 Reply Last reply
      2
      • K kengineer

        I am trying to use a loader object to dynamically load different components I have defined in QML (within the same file). I am finding that I am not able to use anchors in the way I think that I should be able to:

        i.e. simple example, column with 3 rectangles, this works fine in my QML window, as expected, the 3 rectangles are horizontally centered in the parent window:

        Column {
            anchors.horizontalCenter: parent.horizontalCenter
            spacing : 50
        
            Rectangle {
                color: "Red"
                width: 50
                height: 50
            }
        
            Rectangle {
                color: "Red"
                width: 50
                height: 50
            }
        
            Rectangle {
                color: "Red"
                width: 50
                height: 50
            }
        }
        

        However, if I encapsulate this as a component, and use a Loader to load an instance of it, they are no longer centered, the anchor statement is ignored:

        Loader {
        id: myCoolLoader
        anchors.fill: parent
        sourceComponent: myCoolItem
        }
        
        Component {
        id: myCoolItem
        
        Column {
            anchors.horizontalCenter: parent.horizontalCenter
            spacing : 50
        
            Rectangle {
                color: "Red"
                width: 50
                height: 50
            }
        
            Rectangle {
                color: "Red"
                width: 50
                height: 50
            }
        
            Rectangle {
                color: "Red"
                width: 50
                height: 50
            }
        }
        }
        

        How can I make this work or what am I doing wrong here? Maybe since a loader is dynamic the child isn't able to reference the parent the same way? I also tried directly using id "myCoolLoader" rather than parent.

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        @kengineer hi
        see this https://doc.qt.io/qt-5/qml-qtquick-loader.html#loader-sizing-behavior

        you have to set the horizontalCenter of your loader and it will apply to loaded item

        Loader {
        id: myCoolLoader
        anchors.horizontalCenter: parent.horizontalCenter
        sourceComponent: myCoolItem
        }
        
        Component {
        id: myCoolItem
        
        Column {
            
            spacing : 50
        
            Rectangle {
                color: "Red"
                width: 50
                height: 50
            }
        
            Rectangle {
                color: "Red"
                width: 50
                height: 50
            }
        
            Rectangle {
                color: "Red"
                width: 50
                height: 50
            }
        }
        }
        
        1 Reply Last reply
        2

        • Login

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