Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Line Moves Around before finding Center

Line Moves Around before finding Center

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 2 Posters 607 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    oldguardmd
    wrote on last edited by
    #1

    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()
        
    
    Pl45m4P 1 Reply Last reply
    0
    • O oldguardmd

      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()
          
      
      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @oldguardmd

      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.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      O 1 Reply Last reply
      0
      • Pl45m4P Pl45m4

        @oldguardmd

        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.

        O Offline
        O Offline
        oldguardmd
        wrote on last edited by
        #3

        @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:

        Stack Overflow Discussion

        Pl45m4P 1 Reply Last reply
        0
        • O oldguardmd

          @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:

          Stack Overflow Discussion

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @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

          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          2
          • SGaistS SGaist moved this topic from General and Desktop on
          • O oldguardmd has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved