Get GUI element value from QThread
<p>How can a QThread get text from a QLineEdit?</p>

<p>i tried <code>self.t.updateSignal.connect(self.inputedittext.text)</code> to get the QLineEdit value, but I get an error:</p>

<blockquote>
  <p>TypeError: unsupported operand type(s) for +=:
  PyQt4.QtCore.pyqtBoundSignal' and 'int'</p>
</blockquote>

<p>or I get the message:</p>

<blockquote>
  <p>bound signal updateSignal of xxxxxx at 0x02624580</p>
</blockquote>

<p>Code:</p>

<pre><code>import sys
import time
from PyQt4 import QtGui, QtCore

class mc(QtGui.QWidget):

    def __init__(self):
        super(mc,self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('QThread')
        self.inputedittext = QtGui.QLineEdit()
        self.startbutton = QtGui.QPushButton('start')
        self.stopbutton = QtGui.QPushButton('stop')
        self.textlable = QtGui.QLabel('0')

        lv1 = QtGui.QVBoxLayout()
        lb1 = QtGui.QHBoxLayout()

        lb1.addWidget(self.inputedittext)
        lb1.addWidget(self.startbutton)
        lb1.addWidget(self.stopbutton)

        lb2 = QtGui.QHBoxLayout()
        lb2.addWidget(self.textlable)

        lv1.addLayout(lb1)
        lv1.addLayout(lb2)

        self.setLayout(lv1)

        self.t = test_QThread()
        self.t.updateSignal.connect(self.inputedittext.text)

        self.startbutton.clicked.connect(self.start_t)
        self.connect(self.t,QtCore.SIGNAL('ri'),self.setlable)

    def setlable(self,i):
        self.textlable.setText(i)

    def start_t(self):
        self.t.start()
        # print(self.inputedittext.text())


class test_QThread(QtCore.QThread):
    updateSignal = QtCore.pyqtSignal(QtCore.QString)

    def __init__(self):
        QtCore.QThread.__init__(self)

    def run(self):
        i = self.updateSignal
        # i=0
        go = True
        while go:
            i+=1
            time.sleep(1)
            self.emit(QtCore.SIGNAL('ri'),str(i))
            print('run...')




def main():
    app = QtGui.QApplication(sys.argv)
    mw = mc()
    mw.show()
    app.exec_()

if __name__ == '__main__':
    main()
</code></pre>

<p>Use signals to communicate from thread to gui, and from gui to thread:</p>

<pre><code>class mc(QtGui.QWidget):
    ...

    def initUI(self):
        ...

        self.t = test_QThread()
        self.t.progressSignal.connect(self.setlable)
        self.t.requestSignal.connect(self.sendText)

        self.startbutton.clicked.connect(self.start_t)
        self.stopbutton.clicked.connect(self.stop_t)

    def sendText(self):
        self.t.updateSignal.emit(self.inputedittext.text())

    def setlable(self, i):
        self.textlable.setText(str(i))

    def start_t(self):
        self.t.start()

    def stop_t(self):
        self.t.stop()


class test_QThread(QtCore.QThread):
    requestSignal = QtCore.pyqtSignal()
    updateSignal = QtCore.pyqtSignal(str)
    progressSignal = QtCore.pyqtSignal(int)

    def __init__(self, parent=None):
        super(test_QThread, self).__init__(parent)
        self.updateSignal.connect(self.updateSlot)
        self._running = False
        self._count = 0

    def updateSlot(self, text):
        print 'received: "%s", count: %d' % (text, self._count)

    def stop(self):
        self._running = False

    def run(self):
        self._count = 0
        self._running = True
        while self._running:
            self._count += 1
            time.sleep(1)
            self.progressSignal.emit(self._count)
            if self._count % 3 == 0:
                self.requestSignal.emit()
</code></pre>

python pyqt4 signals-slots qthread