PDA

View Full Version : duplicate signal/slot connections check



positive_4_life
14th October 2011, 15:21
I want to prevent a connection between a ui object signal and a slot from occurring if there is already an existing connection.

I can't use Qt::UniqueConnection due to only being able to use 4.5.x, and I can't use QObject::receivers() as a condition in an if statement because it is a protected function.



//doesn't work as protected
if(ui.comboBox->receivers(SIGNAL(currentIndexChanged(int)))
QObject::connect(ui.comboBox,SIGNAL(currentIndexCh anged(int)),this,SLOT(comboBoxchanged(int)))


Any ideas how I could I would be able to check for duplicate connections for these ui objects?

FelixB
14th October 2011, 15:31
maybe you could disconnect the signal first? then you can be sure there is no connection ;)

positive_4_life
14th October 2011, 15:46
I did disconnect the signal, but then some code follows which I can't assure will reconnect or not, and then this code is run which needs to assure a non-duplicate connection, otherwise my slot will evoked multiple times.

stampede
14th October 2011, 17:22
(...) but then some code follows which I can't assure will reconnect or not (...)
So recreate the connection after this code. Another way is to subclass QComboBox, this way you can use the "receivers()" method.

ChrisW67
15th October 2011, 04:52
What is wrong with using the 4th or 5th argument to connect() and setting it to Qt::UniqueConnection?

positive_4_life
17th October 2011, 15:09
What is wrong with using the 4th or 5th argument to connect() and setting it to Qt::UniqueConnection?
Nothing is wrong with that, but the Qt version I am using is only 4.5.x which doesn't have that feature.

I ended up just using blocksignals() instead of disconnecting/reconnecting, as I just wanted to block the signals from triggering the slots when I was programatically setting values for my ui objects. Thanks for your help.

ChrisW67
17th October 2011, 23:15
My bad, missed that in your original post.