- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 pyside,但(我认为)是一个通用的 Qt 问题。
我知道 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
...
这意味着在 loop() 方法中创建的 QTimer 永远不会被事件循环执行。
如果我将设计从 QWaitCondition 更改为 Signals(恕我直言,这是更好的设计),QTimer 可以工作,但我想知道为什么在使用 QWaitCondition 时它们不工作。
最佳答案
要继续处理长时间运行的任务(又名连续循环)中的事件,您需要调用 QCoreApplication::processEvents()
.
这实际上会遍历您的线程的所有排队插槽。
调用此函数对于信号(如果它们是 QueuedConnection 信号/槽连接)也是必需的,以使其离开当前线程并进入另一个线程。
对于 PySides,您需要调用 PySide.QtCore.QCoreApplication.processEvents()
关于python - 如何在使用 QWaitCondition 的 QThread 中使用 QTimer? (皮塞德),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15025644/
我正在使用 pyside,但(我认为)是一个通用的 Qt 问题。 我知道 QThread 实现调用 ._exec() 方法,所以我们应该在启动的 QThread 上有一个事件循环。这样我们就可以在该线
我是一名优秀的程序员,十分优秀!