I want to connect a signal from my custom QWidget class (WidgetClass) to the slot of DerivedController. I try to connect the signal after I instantiate both classes.
However, I get the following error on the connect() statement:
QObject::connect: Cannot connect WidgetClass::mySignal(double) to (nullptr)::mySlot(double)
I'm not quite sure why this is happening. If I understand correctly, you can only connect signals to slots between instances of classes. Below is a pseudo implementation of what I'm writing (it produces the same error in ipython):
from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject
from PyQt5.QtWidgets import QApplication, QWidget
class BaseController():
def __init__(self, widget1, widget2):
self.widget1 = widget1
self.widget2 = widget2
#@pyqtSlot(float)
def mySlot(self, value):
print(f"BaseClass got {value}.")
class DerivedController(BaseController):
def __init__(self, widget1, widget2):
super().__init__(widget1, widget2)
@pyqtSlot(float)
def mySlot(self, value):
#super().mySlot(value)
print(f"Derived class got {value}!")
def _setupConnections(self):
self.widget1.mySignal.connect(
self.mySlot)
self.widget2.mySignal.connect(
self.mySlot)
class WidgetClass(QWidget):
mySignal = pyqtSignal(float)
def emitSignal(self):
self.mySignal.emit(1.8)
def myfunc():
w1 = WidgetClass()
w2 = WidgetClass()
controller = DerivedController(w1, w2)
controller._setupConnections()
controller.widget1.emitSignal()
def main():
app = QApplication(["test"])
return app.exec(myfunc())
if __name__ == "__main__":
main()