Solved Multiple call to QObject::connect
-
Hi, I asked me from a time.
What's appens if I call multiple time QObject::connect with same signal but with different slot?
For example:QPushButton *btn = new QPushButton("Hello World"); connect(btn, SIGNAL(released()), this, SLOT(slot1)); connect(btn, SIGNAL(released()), this, SLOT(slot2)); connect(btn, SIGNAL(released()), this, SLOT(slot3)); connect(btn, SIGNAL(released()), this, SLOT(slot4)); connect(btn, SIGNAL(released()), this, SLOT(slot1));
My intent may be to change slot for same button based on the current context of the program.
Can this solution lead to problems?
Thanks. -
@Stefanoxjx
You may connect as many slots as you wish to a signal.My intent may be to change slot for same button based on the current context of the program.
Whether that's a good idea is a different matter. Don't forget you would also want to disconnect the previous slot if you did this, if you want to change slot.
-
Thanks for answer.
You are right, I don't think to disconnect.
For disconnect I need to call QObject::disconnect with same parameters to connect://To connect connect(btn, SIGNAL(released()), this, SLOT(slot1)); //To disconnect disconnect(btn, SIGNAL(released()), this, SLOT(slot1));
It's correct?
Thanks. -
@Stefanoxjx
Yes that is correct.The alternative is to keep just one slot, maintain information about state in whatever shape or form, and call different methods from the one slot based on the state instead. Avoids disconnecting.
Since you are working with signals/slots: My personal bug-bear/advice is to switch to use New Signal Slot Syntax. Nothing to do with your issue/choice though.
-
Many thanks for clarifications.
I think that I will proceed with one slot that process status and calls multiple methods.Thanks.
Stefano