Search code examples
pythonpyqt5signalssignals-slots

Do signals contain data pyqt5?


I have trouble understanding whether signals contain some data or not. for example the signal windowTitleChanged contains a data of type str which needs to be passed to it's slot:

self.windowTitleChanged.connect(lambda x  : self.onTitle(x)) 

## or self.windowTitleChanged.connect(self.onTitle) which also automatically sends a data

def onTitle(self,k):
        print(k)

While some signals like clicked do not send data unless they are set as checkable (which contain a bool type data). clicking on the pushBotton in the code below does nothing (which surprisingly to me does not raise an error despite the fact that def buttonClicked(self,pressed) requires a pressed argument.

btn2.clicked.connect(self.buttonClicked)

def buttonClicked(self,pressed):
        if pressed:

            self.statusBar().showMessage("l")

It seems to me that some signals like windowTitleChanged contain a data and can be used within the slot, while some contain a None type data (like clicked signal). Is what i've understood correct?


Solution

  • It seems to me that some signals likewindowTitleChanged contain a data and can be used within the slot, while some contain a None type data (like clicked signal). Is what i've understood correct?

    Yes, you are right. You can even see this in action by creating a custom signal.

    from PyQt5.QtCore import pyqtSignal, Qobject
    class Analyzer(QObject):
        analyze_completed = pyqtSignal(bool)
    

    In the above snippet if you emit the analyze_completed signal with a bool the slots that are connected to this signal will receive that bool as a parameter.