简体   繁体   中英

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'> .

The difference between two cases is whether to define qtSignal in the subclass of QObject . But why pyqtSignal will become to pyqtBoundSignal , if it is defined in the subclass of QObject ?

pyqtSignal follows the so called descriptor protocol. 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. The result is the bound signal. This is the same btw for methods of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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