QThread.isFinished returns False in slot connected

2019-07-04 12:37发布

问题:

I have a QThread that emits finished() signal, but its isFinished() returns False. Why is that? How to know when isFinished will start to return True?

from __future__ import print_function
from PySide import QtCore, QtGui

qapp = QtGui.QApplication([])
worker_thread = QtCore.QThread()
worker_thread.start()
worker_thread.quit()

def fin():
    print('isFinished:', worker_thread.isFinished())
worker_thread.finished.connect(fin)
QtCore.QTimer.singleShot(200, qapp.quit)
qapp.exec_()

sometimes it returns true, but most of the time it outputs:

isFinished: False

answer in another SO question(QThread emits finished() signal but isRunning() returns true and isFinished() returns false) suggests that it should be like this:

  1. At this point, isFinished() should start returning true and isRunning() false.
  2. The internals emit the finished() signal.
  • py 2.7.4
  • PySide 1.1.2
  • Qt 4.8.2

also tested with PyQt4

回答1:

Turns out it was a bug in qt (https://bugreports.qt-project.org/browse/QTBUG-30251) which is fixed in Qt 4.8.5. PySide 1.2.1 comes with Qt4.8.5.