我正在尝试将一些参数(循环迭代次数,计数器值等)传递给Qt GUI线程中的run方法。
当我尝试将值传递给run方法时,一切都运行得很好,包括Thread,SIGNAL和SLOT。当我传入一个整数值,例如100,run方法有错误。当在run方法中打印值时,输出是对象存储器地址,例如, < main 。主题为0x02BC27B0&gt ;.
我该如何解决这个问题?。
提前感谢您的帮助。
以下是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_())
答案 0 :(得分:2)
班级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, self.val)
替换(self.val, self)
可以解决您的问题。