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. Retrieving an items position in global screen coordinates and keep it up to date

Retrieving an items position in global screen coordinates and keep it up to date

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 4 Posters 2.0k 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.
  • DuBuD Offline
    DuBuD Offline
    DuBu
    wrote on last edited by
    #1

    Hi all,

    what's the best way to retrieve the position of an item in global screen coordinates and get notified when it changes (i.e. to show the position in a label)?

    MainWindow {
      Item {
        Item {
          Text {
            // text: x coordinate + ", " + y coordinate
          }
        }
      }
    }
    

    The text should get updated when the MainWindow is moved, one of the parent items or the text item itself.

    Cheers!

    raven-worxR 1 Reply Last reply
    0
    • DuBuD DuBu

      Hi all,

      what's the best way to retrieve the position of an item in global screen coordinates and get notified when it changes (i.e. to show the position in a label)?

      MainWindow {
        Item {
          Item {
            Text {
              // text: x coordinate + ", " + y coordinate
            }
          }
        }
      }
      

      The text should get updated when the MainWindow is moved, one of the parent items or the text item itself.

      Cheers!

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @DuBu
      there is simply no generic way to achieve that (in QML)

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      DuBuD 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @DuBu
        there is simply no generic way to achieve that (in QML)

        DuBuD Offline
        DuBuD Offline
        DuBu
        wrote on last edited by
        #3

        @raven-worx To bad. Thx, anyway!

        1 Reply Last reply
        0
        • DuBuD Offline
          DuBuD Offline
          DuBu
          wrote on last edited by
          #4

          And what's the best way to do it without keeping it up to date (i.e. at button click)?

          Cheers!

          1 Reply Last reply
          0
          • GrecKoG Offline
            GrecKoG Offline
            GrecKo
            Qt Champions 2018
            wrote on last edited by GrecKo
            #5

            To do it on demand you can use : http://doc.qt.io/qt-5/qml-qtquick-item.html#mapToGlobal-method

            If you absolutely need it updated live, I believe it's possible to do it in c++ by listening to all the ancestors xChanged, yChanged, widthChanged, heightChanged signals up to the root window.

            or you can call mapToGlobal on a timer.

            DuBuD 1 Reply Last reply
            2
            • GrecKoG GrecKo

              To do it on demand you can use : http://doc.qt.io/qt-5/qml-qtquick-item.html#mapToGlobal-method

              If you absolutely need it updated live, I believe it's possible to do it in c++ by listening to all the ancestors xChanged, yChanged, widthChanged, heightChanged signals up to the root window.

              or you can call mapToGlobal on a timer.

              DuBuD Offline
              DuBuD Offline
              DuBu
              wrote on last edited by
              #6

              @GrecKo I tried it with mapToGlobal, mapFromGlobal, mapToItem, mapFromItem already with no luck.
              But now that you mentioned mapToGlobal again I gave it another try: Now it works and the timer was also a good hint. It's just a hack but anyway.
              Thanks!

              R 1 Reply Last reply
              0
              • DuBuD DuBu

                @GrecKo I tried it with mapToGlobal, mapFromGlobal, mapToItem, mapFromItem already with no luck.
                But now that you mentioned mapToGlobal again I gave it another try: Now it works and the timer was also a good hint. It's just a hack but anyway.
                Thanks!

                R Offline
                R Offline
                red.green
                wrote on last edited by
                #7

                @DuBu Could you please hint a code snippet of me how you implemented getting the global coordinates correctly using mapToGlobal?

                Thanks.

                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