If I have a QGraphicsView and a QGraphicsScene set to it, and I want to add a rectangle:
How can I add it, so it's top left corner will always, no matter how big or small the scene or view is, be in the view's top left corner?
Thanks,
Erlend Graff
Printable View
If I have a QGraphicsView and a QGraphicsScene set to it, and I want to add a rectangle:
How can I add it, so it's top left corner will always, no matter how big or small the scene or view is, be in the view's top left corner?
Thanks,
Erlend Graff
I don't know if I understand the problem correctly but unless you scroll the view an element with position 0,0 will be at the left upper corner of the view.
Well, I will try to explain further:
When I add a rectangle:
That would make the rectangle appear centered in the view.
How can I add it so it would stretch from view coordinates 0,0 to 40,40?
This is because your scene rect is smaller than your view rect. The scene is centered in the view.
Maybe you want to override QGraphicsView::drawForeground and make the rectangle always stay at view coordinates ( 0, 0 ).
Regards
How can I do that?
I tried this:
Code:
eventScene = new scene; eventView->setScene(eventScene); eventScene->setSceneRect(0, 0, 480000, 50);
but when I then scale the whole thing with let's say
the blue box is cut away, because the top left corner is no longer (0,-14) but (0,0)!Code:
eventView->scale(2, 2);
And that is a problem for me. Any help further is really appreciated!
Erlend Graff
Try this:
Code:
QGraphicsItem* rectItem = eventScene->addRect(QRectF(a.x(), a.y(), 100, 12), QPen(Qt::green), QBrush(Qt::blue));
Then, later, when you scale the view:
Code:
eventView->centerOn( itemcenter );
Remember that is the scene rect is larger than the bounding box of your items, and the items are anchored at (0,0), then they will never appear centered in the view. You will have to modify the scene rect for this, or translate your items.
Regards
Also, instead of QGraphicsView::centerOn() you could try to use one of the QGraphicsView::ensureVisible() functions. They all should have the same result.
Regards