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. How to get x, y from an element of ColumnLayout?

How to get x, y from an element of ColumnLayout?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 31 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.
  • M Offline
    M Offline
    michal.19937
    wrote last edited by
    #1

    Hello,
    Consider the code:

    import QtQuick
    import QtQuick.Layouts
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
        ColumnLayout {
            anchors.fill: parent
            Rectangle {
                id: re
                width: 200
                height: 100
                color: "red"
            }
        }
        Rectangle {
            x: mapFromItem(re, 0, 0).x
            y: mapFromItem(re, 0, 0).y
            z: 1
            width: 100
            height: 50
            color: "green"
        }
    }
    

    I'm trying to position the green rectangle on top of the red one, however it always lands in the top left corner of the window. If I replace ColumnLayout with Rectangle, the code works as intended, so ColumnLayout must be the problem.
    How do I get correct x and y of the red rectangle?

    jsulmJ 1 Reply Last reply
    0
    • M michal.19937

      Hello,
      Consider the code:

      import QtQuick
      import QtQuick.Layouts
      
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")
          ColumnLayout {
              anchors.fill: parent
              Rectangle {
                  id: re
                  width: 200
                  height: 100
                  color: "red"
              }
          }
          Rectangle {
              x: mapFromItem(re, 0, 0).x
              y: mapFromItem(re, 0, 0).y
              z: 1
              width: 100
              height: 50
              color: "green"
          }
      }
      

      I'm trying to position the green rectangle on top of the red one, however it always lands in the top left corner of the window. If I replace ColumnLayout with Rectangle, the code works as intended, so ColumnLayout must be the problem.
      How do I get correct x and y of the red rectangle?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote last edited by
      #2

      @michal.19937 Put an item in the layout. In that item you put the red rectangle and then the green one at the same coordinates (whatever the reason you want to do this).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      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