Multiple call to QObject::connect
-
wrote on 31 Jan 2021, 18:13 last edited by
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. -
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.wrote on 31 Jan 2021, 18:19 last edited by@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.
-
wrote on 31 Jan 2021, 18:25 last edited by
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. -
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.wrote on 31 Jan 2021, 18:31 last edited by JonB@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.
-
wrote on 31 Jan 2021, 18:35 last edited by
Many thanks for clarifications.
I think that I will proceed with one slot that process status and calls multiple methods.Thanks.
Stefano
1/5