PDA

View Full Version : How can I make size of QGraphicsScene smaller?



troorl_ua
20th April 2007, 01:21
Hi!
I have some problem...

There are a lot of items on the scene. Their size and arrangement can dynamically vary. But in this case the size of a scene always automatically increase. If the area which items take has decreased, the sizes of the scene does not vary. How is it possible to manage it?:confused:

wysota
20th April 2007, 01:39
After moving an item calculate the bounding rectangle all items occupy by using QRectF::united() on boundingRect()s and resize the scene accordingly.

Of course you may optimise calculations by comparing the original scene size and the element position prior and after a move.

aamer4yu
20th April 2007, 05:45
Hey wysota,,,, he cud also use QGraphicsScene::itemsBoundingRect instead of calculating the united on boiunding rects himself , isnt it ???

wysota
20th April 2007, 07:26
Yes, of course. It could be a bit slower though, but unless there are many many items in the scene, it shouldn't matter.

troorl_ua
20th April 2007, 17:07
Thanks, It's works =)
I used

scene->setSceneRect(0, 0, scene->itemsBoundingRect().width() + offset, scene->itemsBoundingRect().height() + offset);

It's slow, but I don't know other way:(

wysota
20th April 2007, 17:13
As I mentioned before, cache the result from a previous call and update it when an item is moved (based on the move itself).

Bitto
21st April 2007, 08:56
Prefer this:



scene->setSceneRect(scene->itemsBoundingRect());


The itemsBoundingRect() function finds the actual bounding rect, and that might not be at (0, 0). If you force the scene rect to be (0, 0, width, height), you're bound to have items outside the scene.