Why do I need to decorate connected slots with pyqtSlot?

喜你入骨 提交于 2019-11-29 04:21:12
ekhumoro

The main purpose of pyqtSlot is to allow several different overloads of a slot to be defined, each with a different signature. It may also be needed sometimes when making cross-thread connections (see this answer for one such scenario). However, these use-cases are relatively rare, and in most PyQt applications it is not necessary to use pyqtSlot at all. Signals can be connected to any python callable object, whether it is decorated as a slot or not.

The PyQt docs also state:

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

However, in practice, this advantage is generally very small and will often be swamped by other factors. It has very little affect on raw signalling speed, and it would be necessary to make thousands of emits/connections before it started to have a significant impact on cpu load or memory usage. For an in depth analysis of these points, see this Code Project article by @Schollii:

Another advantage is when you design the custom widgets for Qt-designer, methods decorated by pyqtSlot could be selected within Signal/Slot Editor, otherwise you need to write the code.

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