PDA

View Full Version : [SOLVED] Painting QGraphicsItem constant size



JovianGhost
20th March 2010, 19:56
I've created a very simple QGraphicsItem (currently just draws a box) that I'd like to draw the same size each time regardless of zoom level.

What I mean is, if I add two of these items to my scene



scene->addItem(new MyCustomItem());
scene->addItem(new MyCustomItem());
scene->items()[0].setPos(-10, 0);
scene->items()[1].setPos(10, 0);


then start zooming in and out, obviously the item sizes will change depending on zoom level.
What I'd like to do is keep the item sizes visually the same size, for example, the box would be 10 pixels wide on the user's screen, but the zoom still has an effect, i.e. zooming in will make the items appear farther apart.

Is there an easy way to do this?

JovianGhost
20th March 2010, 20:09
The only way I can see at the moment is in my zoom event handler function, to manually iterate through each item and scale it, thusly:



QGraphicsItem* item;
for (int i = 0; i < scene->items().count(); i++)
{
item = scene->items()[i];
if (event->delta() > 0)
item->scale(0.5, 0.5);
else if (event->delta() < 0)
item->scale(2, 2);
}


But there must be an easier way to do this.
I was thinking of keeping track of the scale factor somehow, perhaps in the custom item itself, and then in the paint function take this scale factor into consideration in the paint() event.
But then how would I let each child know that a zoom event occurred?

aamer4yu
20th March 2010, 20:10
How about using QGraphicsItem::setFlags(QGraphicsItem::ItemIgnores Transformations) :rolleyes:

Lykurg
20th March 2010, 20:16
See QGraphicsItem::ItemIgnoresTransformations

EDIT: Another "use reload before you post":rolleyes:

JovianGhost
20th March 2010, 20:18
That's PERFECT, thank you! :cool:

JovianGhost
21st March 2010, 03:36
Ach, I take that back, it's not perfect!

After making this change, when I zoom in, the scrollbars don't appear as they did before! How do I fix this?

Lykurg
21st March 2010, 09:13
did you update your scene rect after zooming?

JovianGhost
22nd March 2010, 02:25
did you update your scene rect after zooming?

Yep, that did the trick, thanks! I thought QGraphicsView would do it for me, but I suppose that flag I set prevents that from happening.


In case anyone stumbles on this thread in the future, here's what I did: after setting the proper scale in the zoom function, I added



// Update the scene's bounds
QRectF rect = scene()->itemsBoundingRect();
if (rect.isNull())
scene()->setSceneRect(QRectF(0, 0, 1, 1));
else
scene()->setSceneRect(rect);