PyQt4 @pyqtSlot: what is the result kwarg for?

我们两清 提交于 2019-11-30 04:55:39

问题


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.



来源:https://stackoverflow.com/questions/11272951/pyqt4-pyqtslot-what-is-the-result-kwarg-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!