PDA

View Full Version : How to find position of QGraphicsView within QgraphicsScene



swbluto
28th October 2009, 19:08
Just a quick question. I'm sure there's a solution out there, but I'm apparently not readily finding the appropriate function for QGraphicsView. See, my scene is much larger than the qgraphicsview (What I see of the scene in the window, as far as my understanding goes) and the user has the choice to scroll around the scene. Is it possible to find out exactly where the view is located in the scene as the user scrolls around and how? I basically need to know the scene coordinates of the view in the top left corner and bottom right corner.

Also, does anyone know how to disable the scroll bars but still have a QGraphicsScene larger than the QGraphicsView? Or... is it possible to load images that are positioned outside the scene? I basically want to preload images before they come into the view, but the user shouldn't be able to scroll into the "preloading images" area on the outside of the view.

EDIT:

I found that one could find the scene coordinates of a view using QGraphicsView's mapToScene(), but now I need to find where a QGraphicItem's position is in the view. How would one do that?

Do I need to get the item's scene, and then I can get the view using one of the scene's methods, and then use the above method? What steps does that take? It doesn't seem like I can get an item's scene or view but I may just be a n00b.

swbluto
29th October 2009, 09:58
It doesn't seem like I can edit my previous post?

Anyways, I found that I could find graphicItems position in the view by finding out what the view's scene coordinates are and comparing it to the graphicItem's coordinate functions which are scene coordinates.

To get the graphicsView from the graphicItem class, though, I had to call scene() and then I had to call views(), I then had to call first() to get the only view from the views list that I assigned to the scene. From there, I used mapToScene(0,0) to get the QGraphicsView's top left coordinate and the right and bottom was calculated using width() and height().

axeljaeger
30th October 2009, 07:12
For me, it sounds strange what you are asking for. You should never bother about things in the view because you can have more than one view on a single scene. If you do calculations based on the view, you will have to do it again for every view. You get proper translated mouse events in the scene. Do all your business in the scene. Also preloading of images into the graphicsscene should not be necessary, at least not something hacky like adding them to the scene but outside of the visible rectangle. Loading the QPixmap to some variable should be enough.

jano_alex_es
30th October 2009, 07:30
I think that's what he wants to do. Prereload an image during startup and showing it when it's inside the view, so he needs the view's position to know when to create them.

By the way, did you find, swbluto, the way to disable the scroll bars?

axeljaeger
30th October 2009, 07:36
Yes I understand that he wants to do that but for me it does not make sense to use that hacky solution to archive that result. Dont do that. Dont try to do hacky things with Qt.




To disable scroll bars, set horizontalScrollBarPolicy and verticalScrollBarPolicy.