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. [SOLVED]Get scene coordinates relative to viewport.
QtWS25 Last Chance

[SOLVED]Get scene coordinates relative to viewport.

Scheduled Pinned Locked Moved General and Desktop
qgraphicssceneqgraphicsviewmapto
10 Posts 4 Posters 9.5k 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.
  • E Offline
    E Offline
    ealione
    wrote on 23 Apr 2015, 12:21 last edited by ealione
    #1

    I have a QGraphicsView that contains a QGraphicsScene. The scene is not entirely visible as it is too big so I have to move it around in order to see parts of the image contained in the scene that are not visible.

    Ideally what I want to achieve is to have coordinates for two rectangles.
    One of them is the rectangle that defines the QGraphicsView's viewport and the other one is a rectangle that will tell me where the scene is relative to the viewport.

    Thus both rectangles will use the same coordinate system as a reference and I can find interesting values like how much of the scene is visible etc.

    Can this be done?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alex_malyu
      wrote on 23 Apr 2015, 23:41 last edited by alex_malyu
      #2

      QGraphicsView provides a set of mapFromScene and mapToScene functions for such purpose.

      1 Reply Last reply
      0
      • E Offline
        E Offline
        ealione
        wrote on 24 Apr 2015, 07:03 last edited by
        #3

        I am aware of those function of curse, but I did not reach what I was hoping for.

        something like

        QRect viewportRect(0, 0, view->viewport()->width(), view->viewport()->height());
        QRectF visibleSceneRect = view->mapToScene(viewportRect).boundingRect();
        

        Or any of the many combinations that I tried did not produce the result I wanted.

        When I drag the image in display for example and you can no longer see its top left corner I would expect to get negative values for the x and y of the rectangle that represents the image, lets call them x1, y1. Those values should get increasingly smaller as I keep moving to the bottom right corner of the image and my visible area gets further from x1, y1.
        Yet all the things I have tried don't really care how I move the image around (or in other words, what part of the image is currently visible).

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Asperamanca
          wrote on 24 Apr 2015, 08:14 last edited by
          #4

          QGraphicsScene::itemsBoundingRect() will return a rectangle over all items in the scene, in scene coordinates.
          Using QGraphicsView::mapFromScene, you can map it to view coordinates. Now you can compare that result to the width and height of your actual viewport. Negative coordinates are outside the view area on left and top.

          1 Reply Last reply
          0
          • E Offline
            E Offline
            ealione
            wrote on 24 Apr 2015, 10:12 last edited by
            #5

            Hi Asperamanca,

            Trying your suggestion I could not observe anything different from what I described above. If I scroll (remember that my picture is bigger than my viewport) then the values you suggest do not change.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Asperamanca
              wrote on 24 Apr 2015, 12:12 last edited by
              #6

              QGraphicsScene::itemsBoundingRect() will always return the same values, unless you change something in the scene itself.

              But QGraphicsView::mapFromScene should take your current scroll position into account. At least it does for me.

              1 Reply Last reply
              0
              • E Offline
                E Offline
                ealione
                wrote on 24 Apr 2015, 12:28 last edited by
                #7

                What I did was

                view-> mapFromScene(view->scene()->itemsBoundingRect());

                But no values ever changed.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Asperamanca
                  wrote on 24 Apr 2015, 12:36 last edited by
                  #8

                  Can you post a specific example and some values? Might be easier to see then.

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    ealione
                    wrote on 24 Apr 2015, 12:58 last edited by
                    #9

                    I am probably tired but I did another test with your solution and it works. My update function had the problem all along, and even though what you said produced the results they only got printed during a resize event.

                    Thanks for the help and sorry for not properly testing your suggestion.

                    F 1 Reply Last reply 20 Jan 2020, 16:56
                    0
                    • E ealione
                      24 Apr 2015, 12:58

                      I am probably tired but I did another test with your solution and it works. My update function had the problem all along, and even though what you said produced the results they only got printed during a resize event.

                      Thanks for the help and sorry for not properly testing your suggestion.

                      F Offline
                      F Offline
                      FluxDice
                      wrote on 20 Jan 2020, 16:56 last edited by
                      #10

                      @ealione You're saying that what you posted on 24 Apr 2015, 03:03 was correct?

                      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