Sanka
Sanka

Reputation: 55

Pass Data into a QThread in Python GUI

I'm trying to pass some parameters (No. of Loop Iterations, counter values, etc) into a run method in a Qt GUI Thread.

Everything work pretty well, including the Thread, SIGNAL and SLOT, expect when I try to pass in a value to the run method. When I pass in an integer value e.g. 100, there is an error at the run method. When print what the value is at the run method, the out put is a Object Memory Address e.g. <main.Main object at 0x02BC27B0>.

How do I fix this?.

Thank you for your help in advance.

Here is a sample of the Python Code:

#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_())

Upvotes: 2

Views: 1865

Answers (1)

Mel
Mel

Reputation: 6065

The class testThread is defined with:

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

Later, you create an instance of testThread like this:

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

So testThread.val is set to self (= the main window), and testThread.parent is set to self.val. Replacing (self, self.val) by (self.val, self) should fix your problem.

Upvotes: 2

Related Questions