2011-10-13 63 views
0

你好,我想弄清楚如何從我的工人類中獲取數據。我運行一個線程處理服務器代碼,我想從我的服務器發送一些數據到PyQt的GUI在變量中使用變量PyQT

我在GUI代碼

 

    self.mytext = QTextEdit() 

,並在我的服務器代碼具有可變我送數據到GUI。唯一的問題是我不知道如何設置的信號到這樣做的權利:-P

 

    self.emit(SIGNAL('mytext'), mytext.setText(msg)) 

任何想法如何做到這一點:-)

*歡呼

回答

2

首先,必須看Signals/Slots概念如何工作。 Original Qt documentation是一個好的開始。然後,如果您正在使用PyQt 4.5+,請嘗試使用new style signals and slots。他們更Pythonic。

下面是一個小例子可能的工作原理(省略了明顯的部分)。

class myWorker(QtCore.QThread): 
    # Register the signal as a class variable first 
    mySignal = QtCore.pyqtSignal(QtCore.QString) 

    # init and other stuff... 

    def someFunction(self): 
     #.... 
     # emit the signal with the parameter 
     self.mySignal.emit(msg) 

# GUI 
class myWindow(QtGui.QMainWindow): 
    def __init__(self): 
     # usual init stuff and gui setup... 
     self.mytext = QTextEdit() 

     # worker 
     self.worker = myWorker() 
     # register signal to a slot 
     self.worker.mySignal.connect(self.mytext.setText) 
+0

謝謝你們兩位的幫助:-) – ADE

1

您需要創建與您定位的插槽具有相同簽名的信號。

self.newText = QtCore.pyqtSignal(QtCore.QString) 

然後將它連接到GUI '的setText' 插槽

self.newText.connect(mytext.setText) 

然後當你在代碼中需要您可以發出它:

self.newText.emit("My Text Here")