Qt-interest Archive, December 2007
QGraphicsView and fitInView()'s strange behaviour
Message 1 in thread
Hi all,
I've got some difficulties understanding how the QGraphics[View/Scene] works.
What I've got is a series of graphicsitems that are placed in a graphicsscene. These appear exactly where they should, and scene->itemsBoundingRect() verifies that for me (it returns the expected values for x(), y(), width() and height()).
I want the view to always display all the items in the scene, but nothing more, though aspect-ratio should be preserved. That basicly means that I want the view first to center on scene->itemsBoundingRect().center() and then "scale" so that scene->itemsBoundingRect() fills the view. I've set view->setResizeAnchor(QGraphicsView::AnchorViewCenter) and that seems to be doing what I want.
When the window (and therefor the view) is resized, the area of the scene defined by scene->itemsBoundingRect() should continue to fill the view.
What I've done to accomplish these objectives is to install an event filter on the view. There I catch QEvent::Resize and QEvent::WindowStateChange and then call a method that should do the centering/scaling. The method gets scene->itemsBoundingRect() as input.
This method looks like this:
zoomOn(QRect rect) {
view->fitInView(rect, Qt::KeepAspectRatio);
}
Currently it works ALMOST as it should, except the view seems to be one step behind. That is, the items stay centered in the view, but when I scale the window (and thereby the view) it seems like the view resizes the content it should display according to some "old" view-sizes. It'll happen like this:
1. The view has one size
2. Resize view and the content still has the same size as earlier
3. Resize again and it obtains the size it _should_ have gotten in 2.
...
n. size==(n-1).size
I assume that this happens because the zoomOn()-method is executed before the view is properly resized (widget-wise). Is this assumtion correct? How can I remedy this behaviour?
Are there any other way of doing what I want that might be better?
Thanks in advance.
Best Regards,
Christopher
--
[ signature omitted ]