Search code examples
pythonsubclasspyside6qtcore

Why can you not create sublclasses of PySide6.QtCore.Signal?


I am working on a project where I need to activate a callback every time a signal is emitted. To achieve this, I created a custom subclass of PySide6.QtCore.Signal. The CallbackSignal class is designed to emit the signal and execute a callback function with the emitted value.

class CallbackSignal(Signal):
    """QT Signal that is emitted with a callback"""

    def __init__(self, returnType: object, callback: Callable):
        super().__init__(returnType)
        self.callback = callback

    def emit(self, val: any):
        super().emit(val)
        if self.callback:
            self.callback(val)

However, when I run the program, I encounter the following error:

File "C:\Python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  ...
TypeError: type 'PySide6.QtCore.Signal' is not an acceptable base type

My question is: Why am I unable to create a subclass of PySide6.QtCore.Signal? Is there is a specific reason for this limitation? If so, what are some alternative solutions to achieve this functionality?

I am open to any suggestions or insights that can help me overcome this. Thank you in advance for your assistance!


Solution

  • Thanks to @mahkitah I realized I can just connect to the signal:

    class Controller(QObject):
        callbackSignal = Signal()
    
        def __init__(callback: Callable):
            super().__init__()
            self.callbackSignal.connect(callback)
    

    Now any time the callbackSignal signal is emitted the callback method is also activated with the same value.