简体   繁体   English

PyQt5中的QObject,pyqtSignal

[英]QObject, pyqtSignal in PyQt5

See the code: 看代码:

from PyQt5.QtCore import QObject, pyqtSignal

class QtSignal(QObject):

    signal = pyqtSignal()

# Case 1:
signal = pyqtSignal()
print(type(signal))

# Case 2:
qtSignal = QtSignal()
print(type(qtSignal.signal))

For case 1 , it outputs <class 'PyQt5.QtCore.pyqtSignal'> , but for case 2 , it outputs <class 'PyQt5.QtCore.pyqtBoundSignal'> . 对于case 1 ,它输出<class 'PyQt5.QtCore.pyqtSignal'> ,但对于case 2 ,它输出<class 'PyQt5.QtCore.pyqtBoundSignal'>

The difference between two cases is whether to define qtSignal in the subclass of QObject . 两种情况之间的区别在于,是否在QObject的子类中定义qtSignal But why pyqtSignal will become to pyqtBoundSignal , if it is defined in the subclass of QObject ? 但是,如果在QObject的子类中定义了pyqtBoundSignal ,为什么会pyqtSignal变成pyqtBoundSignal

pyqtSignal follows the so called descriptor protocol. pyqtSignal遵循所谓的描述符协议。 This is most famously used in the property decorator, but you find plenty of other uses. 这是最著名的用于属性装饰器,但是您可以找到许多其他用途。

The result of this is that accessing an attribute allows it to get a reference to the object you're calling it through. 这样的结果是,访问属性允许它获取对您通过其调用的对象的引用。 So in your case, the instance of QtSignal. 因此,您的情况就是QtSignal的实例。 The result is the bound signal. 结果是绑定信号。 This is the same btw for methods of klasses! 这与klasses的方法相同!

This is needed because when you emit the signal, you do need a sender. 这是必需的,因为当您发射信号时,您确实需要一个发送器。 And this is the way pyqt learns who that is. 这就是pyqt了解谁的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM