PyQt:在closeEvent上终止工作线程

时间:2016-08-18 17:39:10

标签: python user-interface pyqt qthread terminate

我正在使用PyQt编写GUI。 当我按下' X'我想杀死的工作线程。右上角的按钮。 到目前为止我试过了:

class Gui(QtGui.QMainWindow):

def __init__(self):
    super(OneTestGui, self).__init__()
    self.ui = Ui_MainWindow()
    self.ui.setupUi(self)
    self.runner_thread = Runner(ui=self.ui)
    self.runner_thread = QtCore.QThread()
    self.runner_thread.moveToThread(self.runner_thread)
    self.runner_thread.start()

.
.
.

def closeEvent(self, event):
    result = QtGui.QMessageBox.question(self,
                                        "Confirm Exit...",
                                        "Are you sure you want to exit ?",
                                        QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)

    if result == QtGui.QMessageBox.Yes:
        print self.runner_thread.isRunning()
        if self.runner_thread.isRunning():
            self.runner_thread.terminate()
            self.runner_thread.wait()
    else:
        event.ignore()

但是当我按下并且应用程序关闭时,我进入终端:

Qt has caught an exception thrown from an event handler. Throwing
exceptions from an event handler is not supported in Qt. You must
reimplement QApplication::notify() and catch all exceptions there.

我已尝试按照此处的建议安装事件过滤器self,self.ui和self.runner_thread:link但它没有帮助,我不知道在何处以及如何重新实现QApplication ::通知()。

我做错了什么并且有一种正确的方法来杀死跑步者线程?

0 个答案:

没有答案