I can not connect the timer to the move() slot timer.timeout.connect( self.move)
this is not working but QtCore.QTimer.singleShot(50, self.move)
this is just working one step not more.
class Bullet(QGraphicsRectItem):
def __init__(self):
super().__init__()
self.setRect(0,0,10,50)
#timer = QTimer()
#timer.timeout.connect( self.move)
#timer.start(50)
QtCore.QTimer.singleShot(50, self.move)
def move(self):
print("Timer Clicked")
self.setPos(self.x(), self.y()-10)
the problem is simple, a variable created in a function is local and will be eliminated when the function is finished, therefore the signal does not fire, instead the QTimer.singleShot()
has a global scope, the solution is to extend the scope of the timer, for this you must make it a member of the class.
class Bullet(QGraphicsRectItem):
def __init__(self):
super().__init__()
self.setRect(0,0,10,50)
self.timer = QTimer()
self.timer.timeout.connect(self.move)
self.timer.start(50)
def move(self):
print("Timer Clicked")
self.setPos(self.x(), self.y()-10)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.