나는 pyside를 사용하고 있지만 (제 생각에는) 일반적인 Qt 질문입니다.QTimer를 QThread 내부에서 QWaitCondition을 사용하는 방법? (pyside)
QThread 구현에서 ._exec() 메서드를 호출하므로 시작된 QThread에서 이벤트 루프가 있어야합니다. QTimer를이 스레드에서 사용할 수 있습니다. (이 작업은 완벽하게 작동합니다.) 내 문제는 QWaitCondition도 사용되는 경우 QWaitCondition에 (생산자로부터) 알림을 기다리는 무한 루프가있는 "소비자"스레드를 갖고 싶습니다. 내가 가진 문제는이 디자인을 사용하면 소비자 스레드 내부에서 QTimer를 사용할 수 없다는 것입니다.
from PySide import QtGui
from PySide import QtCore
import sys
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.button = QtGui.QPushButton(self)
self.button.setText("Periodical")
self.button.clicked.connect(self.periodical_call)
self.thread = QtCore.QThread()
self.worker = Worker()
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.loop)
self.thread.start()
def closeEvent(self, x):
self.worker.stop()
self.thread.quit()
self.thread.wait()
def periodical_call(self):
self.worker.do_stuff("main window") # this works
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.do_stuff) # this also works
self.timer.start(2000)
def do_stuff(self):
self.worker.do_stuff("timer main window")
class Worker(QtCore.QObject):
def do_stuff_timer(self):
do_stuff("timer worker")
def do_stuff(self, origin):
self.origin = origin
self.wait.wakeOne()
def stop(self):
self._exit = True
self.wait.wakeAll()
def loop(self):
self.wait = QtCore.QWaitCondition()
self.mutex = QtCore.QMutex()
self._exit = False
while not self._exit:
self.wait.wait(self.mutex)
print "loop from %s" % (self.origin,)
self.timer = QtCore.QTimer()
self.timer.setSingleShot(True)
self.timer.timeout.connect(self.do_stuff_timer)
self.timer.start(1000) # <---- this doesn't work
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
frame = MainWindow()
frame.show()
sys.exit(app.exec_())
우리가이 같은 출력을 얻을 버튼을 클릭하면 :
loop from main window
loop from timer main window
loop from timer main window
loop from timer main window
...
이것은 QTimer가 만든 의미를
이
는 시나리오의 조각은 내가 설명하기 위해 노력하고있어입니다 내부 loop() 메서드는 이벤트 루프에 의해 결코 실행되지 않습니다.디자인을 QWaitCondition에서 Signals (더 나은 디자인 imho)로 변경하면 QTimer가 작동하지만 QWaitCondition이 사용될 때 왜 작동하지 않는지 알고 싶습니다.
그의 QWaitCondition 요구 사항은 무엇입니까? – g19fanatic