PyQt4 @pyqtSlot: what is the result kwarg for?

2019-02-13 16:19发布

问题:

By reading this, two questions came up:

1. It says

it is sometimes necessary to explicitly mark a Python method as being a Qt slot

While I always use the @pyqtSlot decorator because it says:

Connecting a signal to a decorated Python method also has the advantage of reducing the amount of memory used and is slightly faster

I ask myself: in which specific cases is it necessary? and: Are there any advantages of not using the @pyqtSlot decorator?

2. The result keyword argument, what is its purpose?

@pyqtSlot(int, result=int)
def foo(self, arg1):
    """ C++: int foo(int) """

It looks like the return value's type, but AFAIK you cannot retrieve return values when emitting signals.

Any ideas about that?

回答1:

  1. It is faster because of PyQt architecture. PyQt converts the python slots to C++ slots, in order to communicate with the Qt framework. When you explicitly mark a Python method as a Qt slot and provide a C++ signature for it, the PyQt code doesn't have to guess the C++ signature itself, as it is already specified. This may enhance performance on heavy projects.

  2. The return value is only needed when you want to call the slot as a normal function.

Edit: It seems that the QMetaObject.invokeMethod method executes a slot and uses it's return value.