fix thread timer

This commit is contained in:
leo 2022-03-09 21:03:50 +01:00
parent dc2e43f257
commit 3b83e1e2e8
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
2 changed files with 26 additions and 9 deletions

10
main.py
View File

@ -13,6 +13,7 @@ from window import Ui_MainWindow
from mainLoop import mainLoop
class MainWindow(QMainWindow):
stopMainLoopTimer = Signal()
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
@ -45,11 +46,13 @@ class MainWindow(QMainWindow):
self.drawTimer.timeout.connect(self.updateFPS)
self.drawTimer.start(1000/60)
self.ui.startButton.clicked.connect(self.mainLoop.timer.start)
self.ui.stopButton.clicked.connect(self.mainLoop.timer.stop)
self.ui.startButton.clicked.connect(self.mainLoop.startTimer)
self.ui.stopButton.clicked.connect(self.mainLoop.stopTimer)
self.stopMainLoopTimer.connect(self.mainLoop.stopTimer)
self.ui.mainFPS_set.valueChanged.connect(self.drawTimer.setInterval)
self.ui.physicsFPS_set.valueChanged.connect(self.mainLoop.timer.setInterval)
self.ui.physicsFPS_set.valueChanged.connect(self.mainLoop.setTimerInterval)
self.fpsTimer = QElapsedTimer()
self.fpsTimer.start()
@ -90,6 +93,7 @@ class MainWindow(QMainWindow):
self.mainLoop.openVehicles(filename[0])
def close(self):
self.stopMainLoopTimer.emit()
self.updateThread.quit()
self.updateThread.wait()
super().close()

View File

@ -1,5 +1,3 @@
import sys
from Map import Map
from CarController import CarController
@ -22,10 +20,7 @@ class mainLoop(QObject):
self.painter.addMap(self.map)
self.painter.addCarController(self.controller)
self.timer = QTimer()
self.timer.timeout.connect(self.update)
self.timer.timeout.connect(self.updateFps)
self.timer.setInterval(1000/60)
self.timer = None
self.fpsTimer = QElapsedTimer()
self.updateFPS.connect(self.parent.updatePhysicsFps)
@ -41,8 +36,26 @@ class mainLoop(QObject):
def start(self):
self.fpsTimer.start()
self.timer = QTimer()
self.timer.timeout.connect(self.updateFps)
self.timer.timeout.connect(self.update)
self.timer.setInterval(1000/60)
self.stopSignal.connect(self.timer.stop)
def startTimer(self):
self.timer.start()
def stopTimer(self):
self.timer.stop()
def setTimerInterval(self,t):
self.timer.setInterval(t)
def quit(self):
super().quit()
self.timer.stop()
@threadSafe
def update(self):
try: