@oldguardmd said in Line Moves Around before finding Center:
I think this person tried to explain why this happens in a Stack Overflow discussion, but I admit that I am still struggling to understand everything they are saying here:
I thought it could be an issue with your sceneRect but wasn't quite sure.
Sometimes you want a fixed scene and sometimes you need it dynamic.
Say your QGraphicsView is a window (a glass window, not a program window) and the QGraphicsScene is, well, a scene/everything you see through the window.
The scene, the world outside, can be (or IS) bigger than the part you see when you look through the window.
What you see depends on how your window (the GView) and our environment (the GScene) are aligned.
Your frame starts at (0 / 0) topleft, but this doesn't mean the topleft point, you "grab" when reaching through your window, is the origin of your environment.
(see Coordinate Mapping)
With no sceneRect (= part of the "world" you want to focus on) the QGraphicsView sets up the scene so that is it centered around your items. And will try to maintain this, when items are moving (which is the case here). As soon as your spinning line comes close to the edge of the visible area (viewPort), the scene will expand and adjust. Probably scrollBars will appear too.
If you set your items for whatever reason to positions like ( 800 / 800 ), the scene will adjust to that area.
When specifying a static sceneRect these adjustments wont occur, but you have to keep track of your items yourself... if they move out of your visible area, they are "gone" until you move them back in or adjust the viewport.
Also the scene only grows, but never shrinks by itself. If you set items to ( -100, -100) and (9000 / 9000) you end up with a pretty huge scene, even if you move both items very close to the origin later.
This is with no fixed scene... (automatically centered viewport around the items at t=0) you can see the viewport "jumping" as the line hits the top edge.
ClockHandNoSceneRect.gif
Same code, but fixed scene here (a bit off because of the circle I painted around the spinning line)
ClockHand22.gif
Hope things became more clear :)
Here is the official Qt GraphicsView documentation
https://doc.qt.io/qt-6/graphicsview.html