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. Finding the coordinates of an item
QtWS25 Last Chance

Finding the coordinates of an item

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 5 Posters 4.8k 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.
  • D Offline
    D Offline
    Deqing
    wrote on last edited by
    #1

    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
    0
    • W Offline
      W Offline
      wladek
      wrote on last edited by
      #2

      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
      0
      • B Offline
        B Offline
        baysmith
        wrote on last edited by
        #3

        @

        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
        0
        • V Offline
          V Offline
          vinb
          wrote on last edited by
          #4

          May i ask why you add 5 to x/y

          1 Reply Last reply
          0
          • B Offline
            B Offline
            baysmith
            wrote on last edited by
            #5

            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
            0
            • V Offline
              V Offline
              vinb
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mbrasser
                wrote on last edited by
                #7

                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
                0
                • D Offline
                  D Offline
                  Deqing
                  wrote on last edited by
                  #8

                  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
                  0
                  • D Offline
                    D Offline
                    Deqing
                    wrote on last edited by
                    #9

                    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
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved