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. What's wrong with centering component not from the main.qml [SOLVED]
Forum Update on Monday, May 27th 2025

What's wrong with centering component not from the main.qml [SOLVED]

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.1k Views
  • 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.
  • S Offline
    S Offline
    sosun
    wrote on 29 Apr 2015, 14:37 last edited by sosun
    #1

    I'm trying to draw rectangle in the center of the document, but can do that only in main.qml, because drawing rectangle in any other draws it on the incorrect position. I have the following main.qml document:

    ApplicationWindow {
        id: root
        width: 1080; height: 1920
    
        Rectangle {
            width: 250; height: 250
            color: "cyan"
            anchors {
                horizontalCenter: parent.horizontalCenter
                verticalCenter: parent.verticalCenter
            }
        }
    
        Button {
            id: myButton
        }
        Loader {
            id: pageLoader
        }
        MouseArea {
            anchors.fill: myButton
            onClicked: pageLoader.source = "Settings.qml"
        }    
    }
    

    This works just fine:

    alt text

    But Settings.qml inserts rectangle in the wrong location on the screen. Is it because qml was loaded from the Loader? I don't know any reasons why it is the way it is, but practice shows that rectangle from the Settings.qml is drawn incorrectly despite the fact that both qmls have the same anchors-positioning code. Here's my Settings.qml:

    Item {
        id: myItem
        width: 1080
        height: 1920
    
        Rectangle {
            id: myRect
            width: 250
            height: 250
            color: "red"
    
            anchors {
                horizontalCenter: parent.horizontalCenter
                verticalCenter:  parent.verticalCenter
            }
        }
    }
    

    And the result (after myButton from the main.qml was pressed) is:

    alt text

    Whether it is somehow relevant to Loader or not?

    P 1 Reply Last reply 29 Apr 2015, 16:28
    0
    • S sosun
      29 Apr 2015, 14:37

      I'm trying to draw rectangle in the center of the document, but can do that only in main.qml, because drawing rectangle in any other draws it on the incorrect position. I have the following main.qml document:

      ApplicationWindow {
          id: root
          width: 1080; height: 1920
      
          Rectangle {
              width: 250; height: 250
              color: "cyan"
              anchors {
                  horizontalCenter: parent.horizontalCenter
                  verticalCenter: parent.verticalCenter
              }
          }
      
          Button {
              id: myButton
          }
          Loader {
              id: pageLoader
          }
          MouseArea {
              anchors.fill: myButton
              onClicked: pageLoader.source = "Settings.qml"
          }    
      }
      

      This works just fine:

      alt text

      But Settings.qml inserts rectangle in the wrong location on the screen. Is it because qml was loaded from the Loader? I don't know any reasons why it is the way it is, but practice shows that rectangle from the Settings.qml is drawn incorrectly despite the fact that both qmls have the same anchors-positioning code. Here's my Settings.qml:

      Item {
          id: myItem
          width: 1080
          height: 1920
      
          Rectangle {
              id: myRect
              width: 250
              height: 250
              color: "red"
      
              anchors {
                  horizontalCenter: parent.horizontalCenter
                  verticalCenter:  parent.verticalCenter
              }
          }
      }
      

      And the result (after myButton from the main.qml was pressed) is:

      alt text

      Whether it is somehow relevant to Loader or not?

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 29 Apr 2015, 16:28 last edited by
      #2

      Hi @sosun,

      Whether it is somehow relevant to Loader or not?

      Yes indeed it is related to the Loader. You must specify position to the Loader. To center the red rectangle you will need to center the Loader itself. Try anchoring the Loader to parent's center.

      Loader {
          id: pageLoader
          anchors.centerIn: parent
      }
      

      157

      S 1 Reply Last reply 29 Apr 2015, 18:08
      2
      • P p3c0
        29 Apr 2015, 16:28

        Hi @sosun,

        Whether it is somehow relevant to Loader or not?

        Yes indeed it is related to the Loader. You must specify position to the Loader. To center the red rectangle you will need to center the Loader itself. Try anchoring the Loader to parent's center.

        Loader {
            id: pageLoader
            anchors.centerIn: parent
        }
        
        S Offline
        S Offline
        sosun
        wrote on 29 Apr 2015, 18:08 last edited by
        #3

        @p3c0 Yeah, it works! Thank you ;)

        1 Reply Last reply
        0

        3/3

        29 Apr 2015, 18:08

        • Login

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