Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    Finding the coordinates of an item

    QML and Qt Quick
    5
    9
    4224
    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.
    • D
      Deqing last edited by

      Is it possible to get the coordinates of an item in the application's coordinate system?

      For example if I click on an item, I can get it's current x and y.

      1 Reply Last reply Reply Quote 0
      • W
        wladek last edited by

        Hi Deqing,

        You may try to use the mouseX and mouseY properties of the MouseArea object.
        Does this work for you?

        Regards,
        Wladek

        1 Reply Last reply Reply Quote 0
        • B
          baysmith last edited by

          @

          import QtQuick 1.0

          Item {
          width: 100
          height: 200

          Rectangle {
              x: 10
              y: 20
              color: "blue"
              width: 10
              height: 10
              MouseArea {
                  anchors.fill: parent
                  onClicked: {
                      console.log("pos " + parent.x + "," + parent.y)
                      parent.x += 5
                      parent.y += 5
                  }
              }
          }
          

          }
          @

          Nokia Certified Qt Specialist.

          1 Reply Last reply Reply Quote 0
          • V
            vinb last edited by

            May i ask why you add 5 to x/y

            1 Reply Last reply Reply Quote 0
            • B
              baysmith last edited by

              Why not? :)

              To show that it displays the current values of x and y, rather than only the starting values. Click on it multiple times and the values change.

              Nokia Certified Qt Specialist.

              1 Reply Last reply Reply Quote 0
              • V
                vinb last edited by

                Aha ok thanks for the answer.
                But if i understand you correctly, its not necessary to add.

                1 Reply Last reply Reply Quote 0
                • M
                  mbrasser last edited by

                  You can use Item's mapToItem function to convert local coordinates to view coordinates (http://doc.qt.nokia.com/4.7/qml-item.html#mapToItem-method).

                  Regards,
                  Michael

                  1 Reply Last reply Reply Quote 0
                  • D
                    Deqing last edited by

                    I'd like to get the x and y of the application's coordinates.

                    Say a flickable item containing a textinput. The text input can be either at the bottom of the screen, or at the top of the screen if user flick it up. These two different situation should get different y.

                    Maybe Michael is right, but only works in C++ code?

                    1 Reply Last reply Reply Quote 0
                    • D
                      Deqing last edited by

                      Just found that mapToItem works, as follows:

                      @var map = textinput.mapToItem(root, 0, 0)@

                      then map.x map.y is the mapped coordinates of the root element.

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