Line Moves Around before finding Center
-
I obtained some simple code from ChatGPT to create a line that just rotates in a circle (similar to a clock). On the first rotation, the line appears to push itself around the screen instead of rotating around a point. Once the line has been pushed to the center of the screen the line rotates around the central point, which is what I want it to do from the very beginning. I cannot figure out how to assign the starting position so the line just rotates around a single point from the beginning.
What am I missing here?
import sys import time import math from PyQt5.QtWidgets import QApplication, QGraphicsScene, QGraphicsView, QGraphicsLineItem from PyQt5.QtCore import Qt, QTimer from PyQt5.QtGui import QPen ARM_LENGTH = 100 ROTATION_SPEED = .01 class RotatingArms(QGraphicsView): def __init__(self): super().__init__() self.scene = QGraphicsScene(self) print(dir(self.scene)) self.setScene(self.scene) self.setGeometry(100, 100, 800, 400) self.angle1 = 0 self.arm1 = QGraphicsLineItem(0, 0, -ARM_LENGTH * math.cos(self.angle1), -ARM_LENGTH * math.sin(self.angle1)) self.arm1.setPen(QPen(Qt.red)) self.scene.addItem(self.arm1) self.timer = QTimer(self) self.timer.timeout.connect(self.update_arms) self.timer.start(16) def update_arms(self): self.angle1 += ROTATION_SPEED self.arm1.setLine(0, 0, ARM_LENGTH * math.cos(self.angle1), -ARM_LENGTH * math.sin(self.angle1)) return app = QApplication([]) window = RotatingArms() window.show() app.exec()
-
Maybe you moved the scene?!
I re-created it (in C++) and cant see the line moving. It stays where you set it and starts rotating. -
@Pl45m4
Well, that seems to have gotten me there. Looks like when the scene is set up, you should specify the geometry.scene_rect = QRectF(-400, -300, 800, 600) # Set the desired sceneRect self.scene = QGraphicsScene(scene_rect, self) self.setScene(self.scene)
Thank you for the help on this.
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:
-
@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 theQGraphicsScene
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) theQGraphicsView
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.
Same code, but fixed scene here (a bit off because of the circle I painted around the spinning line)
Hope things became more clear :)
Here is the official Qt GraphicsView documentation
-
S SGaist moved this topic from General and Desktop on
-