2016-09-19 62 views
1

QThread如何從QLineEdit獲取文本?從QThread獲取GUI元素值

我試圖self.t.updateSignal.connect(self.inputedittext.text)得到QLineEdit的價值,但我得到一個錯誤:

TypeError: unsupported operand type(s) for +=: PyQt4.QtCore.pyqtBoundSignal' and 'int'

或我得到的消息:

bound signal updateSignal of xxxxxx at 0x02624580

代碼:

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

回答

0

使用信號從線程到gui以及從gui到線程的通信:

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() 
+0

謝謝ekhumoro 它的工作,但這是同步GUI和QThread信號它可能從GUI給self._count值?像需要這個QThread運行7次 – user1484400

+0

@ user1484400。對不起,我不明白你在問什麼。 – ekhumoro

+0

這是我的錯,你的文章是答案! – user1484400