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]

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.
  • sosunS Offline
    sosunS Offline
    sosun
    wrote on 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?

    p3c0P 1 Reply Last reply
    0
    • sosunS sosun

      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?

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on 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

      sosunS 1 Reply Last reply
      2
      • p3c0P p3c0

        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
        }
        
        sosunS Offline
        sosunS Offline
        sosun
        wrote on last edited by
        #3

        @p3c0 Yeah, it works! Thank you ;)

        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