Connect a signal to any function?
-
I'm playing with slots/signals. (I'm still learning) I wanted to connect a signal from my own custom object to the maximum value parameter of a QSpinBox. According to the documentation there is no slot for setMaximum on a QSpinBox. But just as a test I tried connecting my signal to the standard, non-slot, setMaximum function of the QSpinBox and it still works.
Is this legal? Is there any reason I shouldn't do this?
-
@Dan203 said in Connect a signal to any function?:
Is this legal?
Yes
Is there any reason I shouldn't do this?
No
Now more verbose reply, there are many options to connect a signal. You can connect a signal to:
- a slot ==> connect(src, SIGNAL(...), dest, SLOT())
- another signal (for example relaying it) ==> connect(src, SIGNAL(...), dest, SIGNAL())
- to a member function of a class ==> connect(src, &SrcClass::signalName, dest, &DestClass::functionName)
- to a functor/lamba ==> connect(src, &SrcClass::signalName, { qDebug() << "signal received"; })
take a look at https://doc.qt.io/qt-5/signalsandslots.html for more details.
-
@KroMignon said in Connect a signal to any function?:
@Dan203 said in Connect a signal to any function?:
Is this legal?
Yes
Is there any reason I shouldn't do this?
No
Now more verbose reply, there are many options to connect a signal. You can connect a signal to:
- a slot ==> connect(src, SIGNAL(...), dest, SLOT())
- another signal (for example relaying it) ==> connect(src, SIGNAL(...), dest, SIGNAL())
- to a member function of a class ==> connect(src, &SrcClass::signalName, dest, &DestClass::functionName)
- to a functor/lamba ==> connect(src, &SrcClass::signalName, { qDebug() << "signal received"; })
take a look at https://doc.qt.io/qt-5/signalsandslots.html for more details.
Thank you. Exactly what I wanted to hear. It's working perfectly in my test and saves me from having to overload every control to add specific slots.