开发者

Using SIgnals with variables PyQT

Hello I am trying to figure out how to get data from my worker class. I have server code running as a threaded process and I want to send some data from my server to the pyQT GUI

I have a variable in my gui code


    self.mytext = QTextEdit()

and in my server code I send the data to the GUI. Only problem is I don't know how to set up the Signals to do this right :-P


    self.emit(SIGNAL('mytext'), mytext.setT开发者_StackOverflow中文版ext(msg))

Any ideas how to do this :-)

*cheers


First, have look at how Signals/Slots concept works. Original Qt documentation for is a good start. Then if you are working with PyQt 4.5+, try to use the new style signals and slots. They are more Pythonic.

Here is how a small example might work (omitting the obvious parts).

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)


You need to create the signal with the same signature as the slot you are targeting.

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

Then connect it to the GUI 'setText' slot

self.newText.connect(mytext.setText) 

And then you can emit it whenever you need to in the code:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜