将数据传递到Python GUI中的QThread中



我正试图将一些参数(循环迭代次数、计数器值等(传递到Qt GUI线程中的运行方法中。

当我尝试将值传递给run方法时,一切都运行得很好,包括Thread、SIGNAL和SLOT。当我传入一个整数值,例如100时,run方法出现错误。当在run方法中打印值时,输出是对象内存地址,例如<main。0x02BC27B0>处的main对象。

我该怎么解决这个问题?。

提前感谢您的帮助。

以下是Python代码的示例:

#class testThread(QtCore.QThread):
    def __init__(self,val, parent=None):
        QtCore.QThread.__init__(self)
        self.pulseTime = 0.002
        self.counter = 0
        self.icount = 0
        self.timeout = 4
        self.default = 99
        self.val = val
        print self.val
    def run(self):
        self.timer = QtCore.QTimer()
        self.timer.setInterval(1000)
        self.timer.setSingleShot(False)
        self.timer.start()
        self.timer.timeout.connect(self.update_mode)
        self.exec_()
    def update_mode(self):
        self.counter += self.val
        self.emit(QtCore.SIGNAL("int"),self.counter)
    def stop(self):
        self.terminate()
class Main(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.ui = Ui_Main()
        self.ui.setupUi(self)
        self._active = False
        self.val = 100
        self.setupSignals()
    def spinBox_Value(self):
        self.val = self.ui.spinBox.value()
    def startPressed(self):
        self.testThreadCall.start()
        if not self._active:
            self._active = True
            self.ui.pButton_StartStop.setText("Stop")
        else:
            self._active = False
            self.testThreadCall.stop()
        print self._active

    def setupSignals(self):
        print "Val", self.val
        self.testThreadCall = testThread(self, self.val)
        self.testThreadCall.setTerminationEnabled(True)
        QtCore.QObject.connect(self.testThreadCall, QtCore.SIGNAL("int"),self.labelSet)
        self.ui.dopDown.addItems(['Default','Second Set','Third Set'])
        self.ui.dopDown.currentIndexChanged.connect(self.dropDownEvent)
        self.ui.pButton_StartStop.clicked.connect(self.startPressed)
        self.ui.spinBox.valueChanged.connect(self.spinBox_Value)
    def closeEvent(self, event):
        self.testThreadCall.quit()
###if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = Main()
    window.show()
    sys.exit(app.exec_())

testThread定义为:

class testThread(QtCore.QThread):
    def __init__(self, val, parent=None):
        ...

稍后,您将创建一个testThread的实例,如下所示:

self.testThreadCall = testThread(self, self.val)

因此testThread.val被设置为self(=主窗口(,并且testThread.parent被设置为self.val。用(self.val, self)替换(self, self.val)应该可以解决您的问题。

相关内容

  • 没有找到相关文章

最新更新