Skip to content Skip to sidebar Skip to footer

Declaration Of The Custom Signals

In Qt, we can create custom signals by making them static variables. and then we use self.signame instead classname.signame. So that creates an instance variable in the class. I wa

Solution 1:

It should be noted that pyqtSignal is declared as an attribute of the class but it is not the same object that is used in the connection as indicated in the docs:

A signal (specifically an unbound signal) is a class attribute. When a signal is referenced as an attribute of an instance of the class then PyQt5 automatically binds the instance to the signal in order to create a bound signal. This is the same mechanism that Python itself uses to create bound methods from class functions.

A bound signal has connect(), disconnect() and emit() methods that implement the associated functionality. It also has a signal attribute that is the signature of the signal that would be returned by Qt’s SIGNAL() macro.

In other words, sig = QtCore.pyqtSignal([bool]) is an unbound signal but self.sig is the bound signal and that can be verified with the following lines:

from PyQt5 import QtWidgets, QtCore


classtest(QtWidgets.QApplication):
    sig = QtCore.pyqtSignal([bool])

    print(type(sig))

    def__init__(self):
        super().__init__([])
        self.window = QtWidgets.QWidget()
        print(type(self.sig))
        self.sig.connect(lambda x=True: print("Hello World"))
        self.bt = QtWidgets.QPushButton(self.window, text="test", clicked=self.sig)
        self.window.setLayout(QtWidgets.QVBoxLayout())
        self.window.layout().addWidget(self.bt)
        self.window.show()


test().exec()

Output

<class'PyQt5.QtCore.pyqtSignal'>
<class'PyQt5.QtCore.pyqtBoundSignal'>

In conclusion, the attribute of the class "sig" is a pyqtSignal that does not have the connect method and that is used to construct the attribute "self.x" which is a pyqtBoundSignal that does have the connect method.

Post a Comment for "Declaration Of The Custom Signals"