Is it possible to position a child object absolutely?



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



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



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



  • 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      
        }
      }
    


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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.