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()
謝謝ekhumoro 它的工作,但這是同步GUI和QThread信號它可能從GUI給self._count值?像需要這個QThread運行7次 – user1484400
@ user1484400。對不起,我不明白你在問什麼。 – ekhumoro
這是我的錯,你的文章是答案! – user1484400