Passing python vars in PyQt using SIGNAL/SLOT...(转载)

So in QT programming signals and slots are at the heart of a well formed application. In Python (using PyQt) it is not exactly obvious how to start developing your own signals and slots. Hopefully this will help...

First, when signals have no arguments things are pretty simple. Take this example:

QObject.connect(self.timer, SIGNAL("timeout()"), self.processStatus)

As you can see, in PyQt you pass string representations of function prototypes in when setting up the connection between a signal and slot. In this case you can just define a simple python function to hook this baby up and the timer will send out the signal when the timeout occurs:

def processStatus(self):
# Do something useful here...

Now what if you are hooking up to a SIGNAL that has arguments from a core C++ based library, either from QT or from QGIS? Still pretty simple:

QObject.connect(self.view, SIGNAL("customContextMenuRequested(const QPoint &)"), self.processCustomMenu)

Note that the string in the SIGNAL definition must match exactly with the signature of the function... a missed space will send you on a wild debugging ride. From here we can just write a simple processCustomMenu function and expect to receive the point from QT as an argument.

def processCustomMenu(self, position):
# Do something useful with the position...

OK... pretty straight forward so far. Now were it gets slightly tricky is if you are defining your own signals and slots and not interacting with QT or QGIS at all. In this case you can follow the simple scenario for signals with no arguments, but if you are defining your own signal that has arguments you have to employ the following trick:

QObject.connect(self.testThread, SIGNAL("testFinished(PyQt_PyObject)"), self.testFinishedFromThread)

As you can see you have to insert a special PyQt_PyObject in the function prototype. This allows you to pass any arbitrary python object in the signal. On the receiving end you can expect to get your argument.

def testFinishedFromThread(self,success):
print "Model Finished with success = ", success

Finally, to send the signal you can just give the same signature followed by the arguments... any python structure can be passed.

self.emit(SIGNAL("testFinished(PyQt_PyObject)"),success)
posted @ 2009-05-21 11:27  firefly_liu  阅读(551)  评论(0编辑  收藏  举报