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. Is it possible to position a child object absolutely?
Forum Updated to NodeBB v4.3 + New Features

Is it possible to position a child object absolutely?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 1.1k Views 1 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.
  • AlienA Offline
    AlienA Offline
    Alien
    wrote on last edited by
    #1

    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
    0
    • AlienA Alien

      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 Offline
      E Offline
      Eeli K
      wrote on last edited by
      #2

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

      AlienA 1 Reply Last reply
      1
      • E Eeli K

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

        AlienA Offline
        AlienA Offline
        Alien
        wrote on last edited by Alien
        #3

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

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Curtwagner1984
          wrote on last edited by
          #4

          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
          1
          • AlienA Offline
            AlienA Offline
            Alien
            wrote on last edited by
            #5

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

            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