Rectangle in scene is changing it's position after multiple times closing - reopening scene window.
-
@tushu said in Rectangle in scene is changing it's position after multiple times closing - reopening scene window.:
So each time, I am adding all small rect in scene and then I am getting scene rect changed, and then I am creating outermost rect and adding it into scene.
So in that case isn't that the answer? Like I said, when you add items to a scene outside its dimensions then scene rectangle will grow to encompass. If you want to analyse the exact behaviour and claim it is wrong/you cannot understand it, then please provide a minimal example (not your current code) illustrating the issue.
@JonB
I am confused.
I am adding objects in scene , then
I am adding outermost rectangle , then
I am closing scene windowNow I am opening scene window again,
and above things will happen again.Then Why scene rectangle will grow ?
scene has been destroyed and opening freshly again. -
@JonB
I am confused.
I am adding objects in scene , then
I am adding outermost rectangle , then
I am closing scene windowNow I am opening scene window again,
and above things will happen again.Then Why scene rectangle will grow ?
scene has been destroyed and opening freshly again.@tushu
Like I said, a minimal example. I can only say that I have usedQGraphicsScene
, including closing and opening windows, and have not noticed anything untoward in thesceneRect()
geometry.QGraphicsScene
does not have any visuals or "window" (onlyQGraphicsView
has that). Don't even know from your description whether you retain the existingQGraphicsScene
and its content or recreate and repopulate again. Like I said, a minimal example.....Meanwhile you might find calling QRectF QGraphicsScene::itemsBoundingRect() const at various stages useful to understand what is going on.
-
So here I want to tell you, I am adding 5 small rectangles and lines etc and then I am adding outermost rectangle in it.
I tried the signal you suggested. It is showing following output.
// here I am adding all small rectangle and lines
// qDebug output of signal you suggested
Scene rect changed
// here I am adding outermost rect and printing its co-oridnates.
top left --> ( 9 , -62 )
bottom right --> (208 , 149)So each time, I am adding all small rect in scene and then I am getting scene rect changed, and then I am creating outermost rect and adding it into scene.
You could try to set a "static"
sceneRect
, so you start with the same setup every time.// topleft is (0/0) now _scene->setSceneRect(0, 0, graphicsView->width(), graphicsView->height()); // Then add your items
Then you also dont have to deal with negative coodinates, unless you want to move or zoom.
-
You could try to set a "static"
sceneRect
, so you start with the same setup every time.// topleft is (0/0) now _scene->setSceneRect(0, 0, graphicsView->width(), graphicsView->height()); // Then add your items
Then you also dont have to deal with negative coodinates, unless you want to move or zoom.
@Pl45m4
@tushu's output shows that the scene's size is changing (as well its top left coordinates). I don't see that as related to whether coordinates are negative or positive? Plus even if you initialise the scene rect to the view's size, that won't be relevant if OP adds new objects which change the scene size.Now, if he is already using something like
setSceneRect(0, 0, graphicsView->width(), graphicsView->height());
that would initialize the scene size to that of the view, and I don't know whether the view's size might be changing as he closes and re-opens the parent window, which could lead to scene size changes with such code. -
@Pl45m4
@tushu's output shows that the scene's size is changing (as well its top left coordinates). I don't see that as related to whether coordinates are negative or positive? Plus even if you initialise the scene rect to the view's size, that won't be relevant if OP adds new objects which change the scene size.Now, if he is already using something like
setSceneRect(0, 0, graphicsView->width(), graphicsView->height());
that would initialize the scene size to that of the view, and I don't know whether the view's size might be changing as he closes and re-opens the parent window, which could lead to scene size changes with such code.I randomly tried this and it worked.
In my creation of outermost rectangle code , there is a line,
QRectF Rect(topLeftX /*+ 3*/, topLeftY /*- 20*/, bottomRightX /*+ 10*/, bottomRightY /*+ 30*/);
Here if I removed +3, -20 +10 and +30 , then my problem gets solved.
But not understanding why it got solved ? What is the reason ?
I had added those numbers to adjust outermost rectangle's width and height.
( Now it's width has reduced little bit, which does not look good. )Please throw some light on this fixation.
-
@Pl45m4
@tushu's output shows that the scene's size is changing (as well its top left coordinates). I don't see that as related to whether coordinates are negative or positive? Plus even if you initialise the scene rect to the view's size, that won't be relevant if OP adds new objects which change the scene size.Now, if he is already using something like
setSceneRect(0, 0, graphicsView->width(), graphicsView->height());
that would initialize the scene size to that of the view, and I don't know whether the view's size might be changing as he closes and re-opens the parent window, which could lead to scene size changes with such code.The scene rectangle defines the extent of the scene. It is primarily used by QGraphicsView to determine the view's default scrollable area, and by QGraphicsScene to manage item indexing.
If unset, or if set to a null QRectF, sceneRect() will return the largest bounding rect of all items on the scene since the scene was created (i.e., a rectangle that grows when items are added to or moved in the scene, but never shrinks).
(https://doc.qt.io/qt-6/qgraphicsscene.html#sceneRect-prop)
@tushu are you really destroying everything? Then the output should be the same.
If not, you add your items to your existing scene, which already was altered according to your items which were added before.I also helps to work with the view dimensions you will get in/after
showEvent
as they are more precise (due to layouts etc.) than the one in your c'tor. -
I randomly tried this and it worked.
In my creation of outermost rectangle code , there is a line,
QRectF Rect(topLeftX /*+ 3*/, topLeftY /*- 20*/, bottomRightX /*+ 10*/, bottomRightY /*+ 30*/);
Here if I removed +3, -20 +10 and +30 , then my problem gets solved.
But not understanding why it got solved ? What is the reason ?
I had added those numbers to adjust outermost rectangle's width and height.
( Now it's width has reduced little bit, which does not look good. )Please throw some light on this fixation.
@tushu
But you don't tell me where you get thetopLeftX, topLeftY , ...
paramters from. If you are saying those are something like the current scene rectangle values then this rectangle is moved from the initial position and has a greater size, doesn't it? So if you then use this e.g. to create aQGraphicsRectItem
which you then add to the scene, as I said the scene's geometry will change to accommodate it. So is this your explanation?Note that if you are only adding this to "make something look nice", you might be better doing some drawing in the
QGraphicsView
renderer instead. Don't add objects to a graphics scene which are not "real" objects for your graphics model. -
@tushu
But you don't tell me where you get thetopLeftX, topLeftY , ...
paramters from. If you are saying those are something like the current scene rectangle values then this rectangle is moved from the initial position and has a greater size, doesn't it? So if you then use this e.g. to create aQGraphicsRectItem
which you then add to the scene, as I said the scene's geometry will change to accommodate it. So is this your explanation?Note that if you are only adding this to "make something look nice", you might be better doing some drawing in the
QGraphicsView
renderer instead. Don't add objects to a graphics scene which are not "real" objects for your graphics model. -
@tushu said in Rectangle in scene is changing it's position after multiple times closing - reopening scene window.:
I am not deleting _scene, after closing scene. I am clearing all the objects in it but not deleting scene.
As gfx items get added to a scene the scene grows to accomodate them, if necessary. However, it does not shrink when you remove or clear them. This is explained in https://doc.qt.io/qt-6/qgraphicsscene.html#sceneRect-prop
If unset, or if set to a null QRectF, sceneRect() will return the largest bounding rect of all items on the scene since the scene was created (i.e., a rectangle that grows when items are added to or moved in the scene, but never shrinks).
At minimum you should perhaps do a
setSceneRect(QRectF())
, but I think this actually limits the scene permanently and then adding items does not grow it. So not what you want.Yes, this might work to reset the size, you could try it. -
@tushu said in Rectangle in scene is changing it's position after multiple times closing - reopening scene window.:
You are right, I am not destroying everything.
I am not deleting _scene, after closing scene. I am clearing all the objects in it but not deleting scene.Yes, that's what I wrote here.
With your additional movement in negative direction hereQRectF Rect(topLeftX /+ 3/, topLeftY /- 20/, bottomRightX /+ 10/, bottomRightY /+ 30/);
you expand your scene by 20. And it stays like this as long as you dont destroy your scene. When you add your items again, your items are not centered in your viewport anymore