sceneRect() not updated immediately after window resizing
-
Hi,
when sceneRect() is updated and how to force it to update?I have this piece of code in a class derived from QGraphicsView
void DisplayAMapGraphicsView::resizeEvent(QResizeEvent *event) { QGraphicsView::resizeEvent(event); qDebug() << Q_FUNC_INFO << "old size:" << event->oldSize() << ", new size:" << event->size(); qDebug() << Q_FUNC_INFO << "rect:" << rect() << ", scene rect:" << sceneRect(); }
When my software start, it has a default size, so this is displayed:
virtual void MyGraphicsView::resizeEvent(QResizeEvent*) old size: QSize(-1, -1) , new size: QSize(400, 582)
virtual void MyGraphicsView::resizeEvent(QResizeEvent*) rect: QRect(0,0 400x582) , scene rect: QRectF(-208,-299 400x582)This is correct.
Now if I maximize the window, this is what I get
virtual void MyGraphicsView::resizeEvent(QResizeEvent*) old size: QSize(400, 582) , new size: QSize(1400, 1002)
virtual void MyGraphicsView::resizeEvent(QResizeEvent*) rect: QRect(0,0 1400x1002) , scene rect: QRectF(752,306 400x582)The size of
rect
is correct, but the size ofsceneRect
is not. It should be (1400x1002) as forrect
. It is not updated.It is not clear to me when sceneRect() is updated.
Do you know how to force sceneRect to update?
-
@odelaune
I may be mistaken, but I don't think so. What makes you think anything would change the size of the scene rectangle after any kind of resizing (e.g. from window size change) of the view? The view refers to what you see visually, so that should resize. But the scene is like an abstract "canvas" which you draw onto, and the view is just a window of arbitrary size onto it --- it might be smaller, equal or larger than the scene, and that's fine. And btw you can have multiple views onto the same scene, one more reason why scene size would not be tied to view size.The only thing which resizes the scene (other than manually) is whenever you add an item to it Qt code finds the minimum bounding rectangle of all items on the scene, taking into account the newly added one. If that is larger than the current scene it enlarges the scene rectangle to encompass; if it is smaller (e.g. after removing/resizing an item) it does not reduce the scene. This is documented. If for some reason you want to enlarge the abstract scene backing canvas to correspond to view changes (don't know why you would) then I think you must do this yourself manually in view size change events.
-
@odelaune said in sceneRect() not updated immediately after window resizing:
The size of rect is correct, but the size of sceneRect is not. It should be (1400x1002) as for rect. It is not updated.
To judge if it's correct or not show your
scene
setup please. -
@Pl45m4
I have no special settings for my QGraphicsScene, but my QGraphicsView has this settings, I.e. all default settings.view->setRenderHint(QPainter::Antialiasing, true); view->setRenderHint(QPainter::TextAntialiasing, true); view->setRenderHint(QPainter::SmoothPixmapTransform, true); view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); view->setFrameStyle(QFrame::NoFrame); view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view->setAttribute(Qt::WA_TranslucentBackground, true); view->setMouseTracking(true);
Any idea?
-
@odelaune said in sceneRect() not updated immediately after window resizing:
view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
This is not the default setting. You could try to comment that line and check again.
How do you set your scene to your view? Does the scene have the view as parent?
Read through this topic. Maybe you find your answer there as we cant see everything related in your project right now.
Edit:
Is it only the fullScreen / maximize behavior or does any resize output a smaller
sceneRect
? -
Commenting
view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
does not change anything for this topic.
I also tried to comment all lines that modify the QGraphicsView, but with no result (the sceneRect is still the old one).Here is how is defined the QGraphicsScene
scene = new QGraphicsScene(this); // "this" is a QWidget view->setScene(scene);
sceneRect
is always smaller just after resizing, not only for full screen or maximized window. -
I think this is not the same problem. Here,
scenereRect
is not updated automatically after resizing while otherrect
is.scenereRect
is updated later but I do not know when, and do not understand what triggers the enwsceneRect
calculation. I will try to make a POC to demonstrate better this behaviour. -
@odelaune
I may be mistaken, but I don't think so. What makes you think anything would change the size of the scene rectangle after any kind of resizing (e.g. from window size change) of the view? The view refers to what you see visually, so that should resize. But the scene is like an abstract "canvas" which you draw onto, and the view is just a window of arbitrary size onto it --- it might be smaller, equal or larger than the scene, and that's fine. And btw you can have multiple views onto the same scene, one more reason why scene size would not be tied to view size.The only thing which resizes the scene (other than manually) is whenever you add an item to it Qt code finds the minimum bounding rectangle of all items on the scene, taking into account the newly added one. If that is larger than the current scene it enlarges the scene rectangle to encompass; if it is smaller (e.g. after removing/resizing an item) it does not reduce the scene. This is documented. If for some reason you want to enlarge the abstract scene backing canvas to correspond to view changes (don't know why you would) then I think you must do this yourself manually in view size change events.
-
You are absolutely right! I was confused between the QGraphicsView and the QGraphicsScene. I just realised that when I was playing with my POC.
Actually, what change the size of the QGraphicsScene is done in another part of my code and is called later. If I recalculate the
sceneRect
according to my needs and callsetSceneRect()
, when my window is resized, everything works as expected.Sorry for the inconvenience.
-