Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Converting between coordinate systems
QtWS25 Last Chance

Converting between coordinate systems

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 819 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.
  • J Offline
    J Offline
    james b-s
    wrote on last edited by
    #1

    I have a mouse position in screen coordinates. It is passed to me from hoverEnterEvent, event->screenPos()
    I have an object on the screen that derives from QGraphicsObject
    I have a point in that object in the item coordinate system.
    I am trying to compare them

    My understanding is that
    myQGraphicsObject->mapToScene(pointInItemCoordinates)
    should return something in screen coordinates
    What I get out of mapToScene is the exact value that I put into mapToScene

    clearly, this isn't the way to do what I need. How can I convert between the coordinate systems

    JonBJ Pl45m4P 2 Replies Last reply
    0
    • J james b-s

      I have a mouse position in screen coordinates. It is passed to me from hoverEnterEvent, event->screenPos()
      I have an object on the screen that derives from QGraphicsObject
      I have a point in that object in the item coordinate system.
      I am trying to compare them

      My understanding is that
      myQGraphicsObject->mapToScene(pointInItemCoordinates)
      should return something in screen coordinates
      What I get out of mapToScene is the exact value that I put into mapToScene

      clearly, this isn't the way to do what I need. How can I convert between the coordinate systems

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @james-b-s
      So for you myQGraphicsObject->mapToScene(0, 0) always (regardless of object position on the scene) returns (0, 0)?

      1 Reply Last reply
      0
      • J james b-s

        I have a mouse position in screen coordinates. It is passed to me from hoverEnterEvent, event->screenPos()
        I have an object on the screen that derives from QGraphicsObject
        I have a point in that object in the item coordinate system.
        I am trying to compare them

        My understanding is that
        myQGraphicsObject->mapToScene(pointInItemCoordinates)
        should return something in screen coordinates
        What I get out of mapToScene is the exact value that I put into mapToScene

        clearly, this isn't the way to do what I need. How can I convert between the coordinate systems

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

        @james-b-s said in Converting between coordinate systems:

        My understanding is that
        myQGraphicsObject->mapToScene(pointInItemCoordinates)
        should return something in screen coordinates

        What I get out of mapToScene is the exact value that I put into mapToScene

        Your understanding is close, but not 100% correct :)

        The devil is in the detail:
        Calling mapToScene from your myQGraphicsObject instance, will use the item as "root" and start the transformation from its POV.
        So mapping a point in some item coordinates to the item's scene coordinate, does nothing, as you've discovered already because from scene's perspective, the item and the point are in the same coordinate system :)

        What you need to do instead:
        Map the global/screen coordinate to your scene coordinate
        and the item coordinate also to scene coordinate
        (or vice versa)
        Then you can compare them.


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        0
        • J Offline
          J Offline
          james b-s
          wrote on last edited by james b-s
          #4

          I have a position in my graphics object and click on it.

          The position in screen coordinates is 950, 413
          The position in graphics object coordinates is 134, 36

          1. Calling mapToScene on the graphics object and passing in the graphics object coordinates yields 134, 36 (unchanged)
          2. Calling mapToScene on the graphics view and passing in the graphics object coordinates yields approx -178, 53
          3. Calling mapToScene on the graphics object and passing in the position in screen coordinates 950, 413 (unchanged)
          4. Calling mapToScene on the graphics view and passing in the position in screen coordinates yields approx 1513, 835

          My understanding is that I should be comparing 2 and 4. Maybe the graphics view is the wrong thing to call mapToScene on?

                QPointF mousePosInSceneCoordinates = nodeGraphicsObject->mapToScene(mousePositionInScreenCoordinates);
                QPointF otherPointInSceneCoordinates = m_pGraphicsView->mapToScene(otherPointInNodeCoordinates);
          
                QPointF mousePosInSceneCoordinates2 = m_pGraphicsView->mapToScene(QPoint(mousePositionInScreenCoordinates.x(), mousePositionInScreenCoordinates.y()));
                QPointF otherPointInSceneCoordinates2 = nodeGraphicsObject->mapToScene(otherPointInNodeCoordinates);
          
          
          JonBJ 1 Reply Last reply
          0
          • J james b-s

            I have a position in my graphics object and click on it.

            The position in screen coordinates is 950, 413
            The position in graphics object coordinates is 134, 36

            1. Calling mapToScene on the graphics object and passing in the graphics object coordinates yields 134, 36 (unchanged)
            2. Calling mapToScene on the graphics view and passing in the graphics object coordinates yields approx -178, 53
            3. Calling mapToScene on the graphics object and passing in the position in screen coordinates 950, 413 (unchanged)
            4. Calling mapToScene on the graphics view and passing in the position in screen coordinates yields approx 1513, 835

            My understanding is that I should be comparing 2 and 4. Maybe the graphics view is the wrong thing to call mapToScene on?

                  QPointF mousePosInSceneCoordinates = nodeGraphicsObject->mapToScene(mousePositionInScreenCoordinates);
                  QPointF otherPointInSceneCoordinates = m_pGraphicsView->mapToScene(otherPointInNodeCoordinates);
            
                  QPointF mousePosInSceneCoordinates2 = m_pGraphicsView->mapToScene(QPoint(mousePositionInScreenCoordinates.x(), mousePositionInScreenCoordinates.y()));
                  QPointF otherPointInSceneCoordinates2 = nodeGraphicsObject->mapToScene(otherPointInNodeCoordinates);
            
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @james-b-s said in Converting between coordinate systems:

            Maybe the graphics view is the wrong thing to call mapToScene on?

            This is the first time you have mentioned graphics view, I don't see that anywhere earlier? Have you read QPointF QGraphicsView::mapToScene(const QPoint &point) const

            Returns the viewport coordinate point mapped to scene coordinates.

            Note viewport. That is not the same as scene. I believe you need to clarify why you are involving the QGraphicsView here?

            1 Reply Last reply
            0
            • J Offline
              J Offline
              james b-s
              wrote on last edited by
              #6

              I am involving graphicsView because it is the only object that I know of to call mapToScene on other than the graphics object. What object should I be calling mapToScene on?

              And I'm a bit shocked to discover that mapToScene of a Graphics item maps to the scene, but that mapToScene on a graphicsView maps to something else.

              But it still boils down to what objects should I be calling mapToScene on?

              JonBJ 1 Reply Last reply
              0
              • J james b-s

                I am involving graphicsView because it is the only object that I know of to call mapToScene on other than the graphics object. What object should I be calling mapToScene on?

                And I'm a bit shocked to discover that mapToScene of a Graphics item maps to the scene, but that mapToScene on a graphicsView maps to something else.

                But it still boils down to what objects should I be calling mapToScene on?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @james-b-s
                I am sorry but I don't have time now to answer, and I have to think about this stuff too! But there are 3 types of coordinates:

                • QGraphicsItem co-ordinates are relative to the the graphics item. (0.0) is the top left of the item.
                • QGraphicsScene co-ordinates are relative to the scene. (0,0) is the center of the scene.
                • QGraphicsView co-ordinates are relative to the view's viewport. (0,0) is the top-left of the view. The view may be scrolled or scaled, and I am unsure how that affects them. (0,0) may be the top-left of the "screen" as you look at it, or it may be changed by any scrolling etc.

                I asked you earlier, from what I understood you to be saying ("What I get out of mapToScene is the exact value that I put into mapToScene"), whether

                So for you myQGraphicsObject->mapToScene(0, 0) always (regardless of object position on the scene) returns (0, 0)?

                I would not expect that to be the case unless your object is placed at (0,0) on the scene.

                Sorry, gotta go, maybe someone will help you more. Have a play with various values/calls while you get the hang of it!

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  james b-s
                  wrote on last edited by
                  #8

                  I have not tried it specifically passing 0, 0 to mapToScene. The graphics object in question is at position 0,0 in the scene. I am clicking into a known position within that graphics object. When I do, I have two known values. The mouse position in "screen" coordinates and that known position in graphics objects coordinates.

                  JonBJ Pl45m4P 2 Replies Last reply
                  0
                  • J james b-s

                    I have not tried it specifically passing 0, 0 to mapToScene. The graphics object in question is at position 0,0 in the scene. I am clicking into a known position within that graphics object. When I do, I have two known values. The mouse position in "screen" coordinates and that known position in graphics objects coordinates.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @james-b-s said in Converting between coordinate systems:

                    The graphics object in question is at position 0,0 in the scene.

                    Then of course myQGraphicsObject->mapToScene(0, 0) will return (0, 0) for the scene coordinate, and (100, 100) would equally return (100,100)! But if the item/object is not at (0,0) on the scene then it will return a different value.

                    I think for a mouse position on the view you would use QGraphicsView->mapToScene() to get the scene coordinate. This has nothing to do with any QGraphicsItem. Map that to scene too if you want to compare. @Pl45m4 may have said this earlier. Perhaps he can sort you out :)

                    1 Reply Last reply
                    0
                    • J james b-s

                      I have not tried it specifically passing 0, 0 to mapToScene. The graphics object in question is at position 0,0 in the scene. I am clicking into a known position within that graphics object. When I do, I have two known values. The mouse position in "screen" coordinates and that known position in graphics objects coordinates.

                      Pl45m4P Offline
                      Pl45m4P Offline
                      Pl45m4
                      wrote on last edited by Pl45m4
                      #10

                      @james-b-s said in Converting between coordinate systems:

                      When I do, I have two known values. The mouse position in "screen" coordinates and that known position in graphics objects coordinates.

                      And where do you want to compare them?
                      In your item, in your scene or in a different place?

                      Usually the task is simple, but can be quite confusing as I've mentioned in my reply before.

                      You have six options:

                      • when you compare in your item class (directly in hoverEnterEvent, for example):

                        • use mapFromScene( event->scenePos() ) and compare with your raw item point
                          OR
                        • use mapToScene( itemPoint ) on your item point and compare with event->scenePos()
                      • when comparing in your custom scene or view class:

                        • get your event point to the scene class somehow (e.g. send via signal), then
                          • map both to scene coords... using the source as origin, e.g. item->mapToScene or this->mapToScene
                            OR
                          • map scene's sceneCoords to item's coordinate system using item->mapFromScene( scenePoint), then compare with your raw item point in item coords.
                      • when comparing globally (in MainWindow):

                        • use event->screenPos() in the item's event handler, send it to your destination and use mapToGlobal( point ) for the other point in your widget... then you can compare them both in screen coords.
                          OR
                        • access the view/scene from there and let them transform the global point in scene coords with mapToScene (this might need a mapFromGlobal on the point before), then compare them in scene coords

                      HoverEnter01.gif

                      (Had an example ready where it was easy to add)

                      The item is 100x100 with top-left being (0 / 0).
                      Inner rect (red box) is 50x50, starting at (50 / 50) in item coordinates.

                      As you can tell from the ItemPos and ScenePos point, the item's top-left (0 / 0) is at (5 / 66), which is the offset for this item in the scene.
                      Screen position should be clear ( = actual position in my device's screen coordinate system)

                      void myObj::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
                      {
                          QPoint screenPoint = event->screenPos();
                          QPointF scenePoint = event->scenePos();
                          QPointF itemPoint = event->pos();
                      
                          qDebug() << "HoverEnter ScenePos: " << scenePoint;
                          qDebug() << "HoverEnter ScreenPos: " << screenPoint;
                          qDebug() << "HoverEnter ItemPos: " << itemPoint;
                      
                          // innerRect = QRectF(50, 50, 50, 50)
                          if (innerRect.contains( mapFromScene(scenePoint) ))
                              qDebug() << "HoverEnter hits Rect";
                          else
                              qDebug() << "Rect not hit";
                      
                          // OR
                          // local item point (1, 25) mapped to scene and compared with scene event coords
                          // if (mapToScene(1, 25) == scenePoint)
                          //     qDebug() << "Match single point (1, 25)";
                          // else
                          //     qDebug() << "No hit on (1, 25)";
                      
                          QGraphicsItem::hoverEnterEvent(event);
                      }
                      

                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      1 Reply Last reply
                      1
                      • JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

                        @james-b-s
                        Hopefully @Pl45m4's excellent reply resolves everything for you. I think you are in the first situation he mentions. I would be tempted to use mapToScene() and then do everything in scene coordinates, easiest to conceptualize.

                        Just going back to your original post, in case it helps sort you out:

                        My understanding is that
                        myQGraphicsObject->mapToScene(pointInItemCoordinates)
                        should return something in screen coordinates
                        What I get out of mapToScene is the exact value that I put into mapToScene

                        • mapToScene() returns scene coordinates. They are not the same as screen coordinates. (Screen coordinates always involve QGraphicsView, even if implicitly.)
                        • You only happen to get myQGraphicsObject->mapToScene(pointInItemCoordinates) == pointInItemCoordinates because your myQGraphicsObject->pos() == QPointF(0, 0) AND myQGraphicsObject->parent() == nullptr or equivalently myQGraphicsObject->scenePos() == QPointF(0, 0). If your item is not at (0,0) on the scene you would not get this identity result.
                        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