PySide 插槽装饰器是必需的吗? [英] Is the PySide Slot Decorator Necessary?

查看:102
本文介绍了PySide 插槽装饰器是必需的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一些使用@QtCore.Slot 装饰器的 PySide 插槽示例代码,有些则没有.自己测试了一下,好像没什么区别.有什么理由我应该或不应该使用它吗?例如,在以下代码中:

I've seen some example code for PySide slots that uses the @QtCore.Slot decorator, and some that does not. Testing it myself, it doesn't seem to make a difference. Is there a reason I should or should not use it? For example, in the following code:

import sys
from PySide import QtCore

# the next line seems to make no difference
@QtCore.Slot()
def a_slot(s):
    print s

class SomeClass(QtCore.QObject):
    happened = QtCore.Signal(str)
    def __init__(self):
        QtCore.QObject.__init__(self)
    def do_signal(self):
        self.happened.emit("Hi.")

sc = SomeClass()
sc.happened.connect(a_slot)
sc.do_signal()

@QtCore.Slot 装饰器没有区别;我可以省略它,调用@QtCore.Slot(str),甚至@QtCore.Slot(int),它仍然很好地说,嗨."

the @QtCore.Slot decorator makes no difference; I can omit it, call @QtCore.Slot(str), or even @QtCore.Slot(int), and it still nicely says, "Hi."

PyQt 的 pyqtSlot 似乎也是如此.

The same seems to be true for PyQt's pyqtSlot.

推荐答案

链接解释了以下内容关于 pyqtSlot 装饰器:

This link explains the following about the pyqtSlot decorator:

虽然 PyQt4 允许任何 Python 可调用在以下情况下用作插槽连接信号,有时需要明确标记一个Python 方法作为 Qt 插槽并提供 C++ 签名它.PyQt4 提供了 pyqtSlot() 函数装饰器来做到这一点.

Although PyQt4 allows any Python callable to be used as a slot when connecting signals, it is sometimes necessary to explicitly mark a Python method as being a Qt slot and to provide a C++ signature for it. PyQt4 provides the pyqtSlot() function decorator to do this.

将信号连接到修饰的 Python 方法也有减少内存使用量的优点是稍微更快.

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

由于 pyqtSlot 装饰器可以接受额外的参数,例如 name,它允许不同的 Python 方法来处理信号的不同签名.

Since the pyqtSlot decorator can take additional argument such as name, it allows different Python methods to handle the different signatures of a signal.

如果不使用槽装饰器,信号连接机制必须手动计算所有类型转换,以从底层 C++ 函数签名映射到 Python 函数.当使用槽装饰器时,类型映射可以是显式的.

If you don't use the slot decorator, the signal connection mechanism has to manually work out all the type conversions to map from the underlying C++ function signatures to the Python functions. When the slot decorators are used, the type mapping can be explicit.

这篇关于PySide 插槽装饰器是必需的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆