What do you do if you think you’ve correctly connected a signal to a slot and yet your slot’s not being fired? I ran into that Friday at 5:30pm, just about the time I hoped I’d call it a day and go home. Fortunately in my case, I was simply passing null
to connect, and I got a message in the debugging log about trying to connect a signal from a null object. (Unfortunately for me, I didn’t see the log right away, because of all the other stuff I was dumping to the log.)
If you’re in the same boat, what do you do to debug the situation? Here’s a bunch of things to try:
- Check the compile log for error messages about undefined signals and slots.
- Check the run-time log for errors on connection. If need be, ensure that connect succeeds by testing its return value (it should return
true
. - Check to make sure that the connect code is reached, that the emit code is reached, and the slot code. (Odds are one of them isn’t.)
- Make sure you declare the connection like this:
connect(sender, SIGNAL(someSignal(type)), receiver, SLOT(received(type)))
The signal and slot specifications should have the argument types, but not the arguments themselves. Moreover, you can omitconst
and reference specifications in the arguments; the meta-object compiler strips them anyway. - Check the parameters of the signal and slot and ensure that they match precisely
- Check to be sure that you’ve correctly declared the
signals
andslots
portions of your header correctly. - For that matter, be sure that your sender and receiver both inherit from
QObject
, and that you haveQ_OBJECT
declared in your class definition. (Remember, you need to do both.) - If you’ve forgotten part of the
QObject
declaration in your header, re-run qmake and rebuild. - Make sure you make the connection with
connect
before you invoke any functions that fire the signal. Signals may fire synchronously. - Make sure you’re not disconnecting the signal anywhere with
disconnect
.
Usually, the problem’s pretty easy to track down, especially if you check the log and the signal and slot declarations closely. A common mistake is to try to wire a signal to a slot that’s not been declared a slot, too, so check your headers closely!