Python PYQT如何将数据从MainUiWindows获取到QThread?
我想知道如何从MainUiWindows(如QlineEdit)发送信息,并将其发送到QThread。我需要它在实时,我想改变信息,每次我想和它改变变量在该QThread 我目前的情况是:Python PYQT如何将数据从MainUiWindows获取到QThread?,python,pyqt,qthread,Python,Pyqt,Qthread,我想知道如何从MainUiWindows(如QlineEdit)发送信息,并将其发送到QThread。我需要它在实时,我想改变信息,每次我想和它改变变量在该QThread 我目前的情况是: class ThreadClassControl(QtCore.QThread): def __init__(self): QThread.__init__(self) self.ui=MainUiClass() def run(self):
class ThreadClassControl(QtCore.QThread):
def __init__(self):
QThread.__init__(self)
self.ui=MainUiClass()
def run(self):
print self.ui.QlineEdit.text()
但是有了它,我只在这个线程启动时获得信息,正如我所说的,我希望在她的迭代之间更改这个变量
感谢AdvanceQt小部件不是线程安全的,您不应该从主线程之外的任何线程访问它们(您可以在Qt文档中找到更多详细信息)。使用线程和Qt小部件的正确方法是通过信号/插槽 要将GUI的值分配给第二个线程,需要将它们从主线程分配给该线程(参见[1]) 如果希望在线程中修改该值,则需要使用信号(请参见[2])
类主线程(QtGui.QMainWindow,Ui\u MainWindow):
...
def uuu init uuu(self,parent=None):
...
#创建QLineEdit实例并分配字符串
self.myLine=QLineEdit()
self.myLine.setText(“你好,世界!”)
#创建线程实例并连接到函数的信号
self.myThread=ThreadClassControl()
self.myThread.lineChanged.connect(self.myLine.setText)#你好,乔治。如果我的答案有用,请不要忘记将其标记为正确。谢谢!:)
class MainThread(QtGui.QMainWindow, Ui_MainWindow):
...
def __init__(self, parent = None):
...
# Create QLineEdit instance and assign string
self.myLine = QLineEdit()
self.myLine.setText("Hello World!")
# Create thread instance and connect to signal to function
self.myThread = ThreadClassControl()
self.myThread.lineChanged.connect(self.myLine.setText) # <--- [2]
...
def onStartThread(self):
# Before starting the thread, we assign the value of QLineEdit to the other thread
self.myThread.line_thread = self.myLine.text() # <--- [1]
print "Starting thread..."
self.myThread.start()
...
class ThreadClassControl(QtCore.QThread):
# Declaration of the signals, with value type that will be used
lineChanged = QtCore.pyqtSignal(str) # <--- [2]
def __init__(self):
QtCore.QThread.__init__(self)
def __del__(self):
self.wait()
def run(self):
print "---> Executing ThreadClassControl"
# Print the QLineEdit value assigned previously
print "QLineEdit:", self.line_thread # <--- [1]
# If you want to change the value of your QLineEdit, emit the Signal
self.lineChanged.emit("Good bye!") # <--- [2]