Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Is it possible to position a child object absolutely?

    QML and Qt Quick
    3
    5
    736
    Loading More Posts
    • 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.
    • Alien
      Alien last edited by

      Hi,
      Look at the below code :

      ApplicationWindow {
          id:root
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")
      
        Rectangle{
            id:parentRectID
            width: 320
            height: 240
            color: "white"
            border.color: "red"
            border.width: 2
            /*-------------------------------*/
            Rectangle
            {
              id:childID
              width: parent.width/2
              height: parent.height/2
              color: "orange"
              anchors.centerIn: parent
              /*-------------------------------*/
              Rectangle
              {
                id:grandChildID
                width: parent.width/2
                height: parent.height/2
                color: "lightblue"
                x:320
                y:240
                /*I want to use root positioning like otherRectID*/
              }
            }
        }
      
        Rectangle
        {
          id:otherRectID
          width: 100
          height: 100
          color: "red"
          x:320
          y:240
        }
      }
      

      How to make grandChildID positioning from root space?

      E 1 Reply Last reply Reply Quote 0
      • E
        Eeli K @Alien last edited by

        @Alien See Item.mapFromItem and Item.mapToItem and "Concepts - Visual Coordinates in Qt Quick" in Qt docs.

        Alien 1 Reply Last reply Reply Quote 1
        • Alien
          Alien @Eeli K last edited by Alien

          Dear @Eeli-K ,
          Is there any examples exist to show how can i use them(Item.mapFromItem and Item.mapToItem )

          1 Reply Last reply Reply Quote 0
          • C
            Curtwagner1984 last edited by

            said in Is it possible to position a child object absolutely?:

            id:grandChildID

            grandChildID x and y parameters are relative to its parent. You can use mapToItem like @Eeli-K said to set the x and y parameters relative to root in the following way: (Assuming you want to set grandChildID to (50,50) relative to root)

            ApplicationWindow {
                id:root
                visible: true
                width: 640
                height: 480
                title: qsTr("Hello World")
            
                Component.onCompleted{
                  setGrandChildAbsolutCords();
                }
                ...
                    Rectangle
                    {
                      id:grandChildID
                      width: parent.width/2
                      height: parent.height/2
                      color: "lightblue"
                      // x:320
                      // y:240
                      /*I want to use root positioning like otherRectID*/
                    }
                  }
            
                function setGrandChildAbsolutCords(){
                  // will translate root (50,50) coordinates to grandChildID item coordinates
                  var cords = grandChildID.mapFromItem(root,50,50); 
                  grandChildID.x = cords.x
                  grandChildID.y = cords.y      
                }
              }
            
            1 Reply Last reply Reply Quote 1
            • Alien
              Alien last edited by

              Dear @Eeli-K and @Curtwagner1984 Thanks it works fine.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post