PDA

View Full Version : Auto scale a QGraphicsView



niluje
5th July 2011, 18:20
Hi,

I have a QGraphicsScene containing a few items. The corresponding QGraphicsView has no scrollbar so you have to use the mouse to view items which are not visible on the screen.

I would like to create a button which, when clicked, centers the scene and scales it in order to view all items at a time.

What's the best way to achieve that?


Regards,

d_stranz
5th July 2011, 20:01
QGraphicsView::fitInView( scene()->sceneRect(), Qt::KeepAspectRatio );

Answered less than a week ago in another post. Using the forum's search feature could save you a lot of time waiting for a reply.

niluje
5th July 2011, 20:36
Thanks a lot for your time.
I used scene()->itemsBoundingRect() instead of ->sceneRect(), but every works fine now.

I'm sorry for this topic which seems to be recurrent. I searched on google and here, and I couldn't find anything. Bad keywords I guess !

Thanks again,

d_stranz
5th July 2011, 21:11
See the docs - using itemsBoundingRect() could be an expensive operation if there are many items. If you are doing this during a resize event, then resizing might have bad performance problems since the scene would have to calculate the bounding rect for you, then transform everything to redraw it in the new size. Scene rect is basically a constant, so the scene only needs to calculate what is needed for the resize.

If your scene contents are mostly static (don't change, don't move around), then after populating the scene, you could set the scene rect to the itemsBoundingRect() and avoid the calculations.

niluje
5th July 2011, 23:43
I understood that there itemsBoundingRect() is an expensive operation.
But my scene is often changing and there isn't so much items in it: one hundred for, and later maximum 10 000. That's not *so* much is it?