从QThread打开QDialog并获取返回数据?

时间:2017-02-07 12:22:31

标签: qt pyqt pyside qthread qdialog

我有一个场景,我将执行部分外包给QThread,在执行期间我需要启动QDialog以在QThread中获取一些用户输入。

对于QDialog调用不在图片中的情况工作正常,但对于QDialog代码运行的片段,我得到了

QPixmap: It is not safe to use pixmaps outside the GUI thread
QObject::startTimer: timers cannot be started from another thread
QApplication: Object event filter cannot be in a different thread.

并且执行突然停止。

我的QDialog代码:

class ResultDialog(QDialog):
    def __init__(self, parent = None):
        super(ResultDialog, self).__init__(parent)

        self.resultIndex = -1
        grid_layout = QGridLayout(self)

        failreplace_layout = QHBoxLayout(self)
        faildontreplace_layout = QHBoxLayout(self)

        self.info_lbl = QLabel(self)
        self.info_lbl.setText("Image comparision failed for screen resolution")

        self.failreplace_radio = QRadioButton(self)
        self.failreplace_radio.setText("Fail and Replace Reference Image")
        self.faildontreplace_radio = QRadioButton(self)
        self.faildontreplace_radio.setText("Fail and Do not replace Reference Image")

        self.tester_comment = QPlainTextEdit(self)
        self.tester_comment.clear()
        self.tester_comment.setPlainText('Tester comment is desired')
        self.tester_comment.setDisabled(True)

        self.buttonsend = QPushButton(self)
        self.buttonsend.setText("Ok")
        self.buttonsend.setCheckable(True)

        grid_layout.addWidget(self.info_lbl,0,0)
        grid_layout.addWidget(self.failreplace_radio,1,0)
        grid_layout.addWidget(self.faildontreplace_radio,2,0)
        grid_layout.addWidget(self.tester_comment,3,0)

        self.loop = QtCore.QEventLoop()

        # OK and Cancel buttons
        grid_layout.addWidget(self.buttonsend, 4,0)

        self.buttonsend.clicked.connect(self.submitclose)
        self.failreplace_radio.clicked.connect(self.onfailreplace_radio)
        self.faildontreplace_radio.clicked.connect(self.onfaildontreplace_radio)

    def submitclose(self):
        self.loop.quit()
        self.accept()

    def onfailreplace_radio(self):
        print "onfailreplace_radio "
        self.tester_comment.setDisabled(False)
        self.buttonsend.setDisabled(False)
        self.tester_comment.clear()
        self.resultIndex = 0

    def onfaildontreplace_radio(self):
        print "onfaildontreplace_radio "
        self.tester_comment.setDisabled(False)
        self.buttonsend.setDisabled(False)
        self.tester_comment.clear()
        self.resultIndex = 1

    # static method to create the dialog and return
    @staticmethod      
    def returnSelection(parent):
        dialog = ResultDialog(parent)
        result = dialog.show()
        dialog.loop.exec_();

        print "dialog.buttonsend.isChecked() ", dialog.buttonsend.isChecked()
        if dialog.buttonsend.isChecked() == True:
            if not str(dialog.tester_comment.toPlainText()):
                QMessageBox.critical(dialog, 'Tester Comment', 'Tester comment is desired')
                return (dialog.resultIndex, 'NA')
            else:
                return (dialog.resultIndex, str(dialog.tester_comment.toPlainText()))

if __name__ == "__main__":
    app = QApplication([])
    ok = ResultDialog.returnSelection()
    print("{}".format(ok))
    app.exec_()

我从Qthread.run()调用它,如:

index, testercomment = ResultDialog.returnSelection(None)
    if index == 0 or index == 1:
        self.resumeExec()                                            
    else:
        self.sync.lock();    
        self.pauseCond.wait(self.sync)
        self.sync.unlock();    

0 个答案:

没有答案