PDA

View Full Version : Populating, signals and comboboxes



ShamusVW
11th August 2010, 09:15
Hello
I have a signal/slot connected to a combobox


SIGNAL(currentIndexChanged(QString))

However, when I initially populate the combobox, it responds to this signal, and calls the slot.
How can I "switch off" the signaling from the combobox while populating it. I only want it to activate when the index is changed due to a user selecting an entry.

I looked for an "enabled" property, but couldn't find one, thinking to disable it, populate it, then enable it again.

Thanks.

Talei
11th August 2010, 09:30
Try disconnect(qcombobox...), then populate, and connect(qcombobox...) again.
Or use QStandardItemModel, add items to the model and setModel on the qcombobox.

Zlatomir
11th August 2010, 09:31
You can create the connection only after the combobox is populated, or you can block signals (http://doc.trolltech.com/4.6/qobject.html#blockSignals) or disconnect (http://doc.trolltech.com/4.6/qobject.html#disconnect-2).

But my advice is to do the first (populate before connection), it's also a good practice to initialize variables as soon as possible.

ShamusVW
11th August 2010, 09:39
Thanks. Did it the first way, works well.

ShamusVW
11th August 2010, 10:48
Actually, will do

comboBox->blockSignals(true)

then populate, then

comboBox->blockSignals(false)

Seems more elegant!

Zlatomir
11th August 2010, 11:39
For me, the most elegant design is the initialization of that variable (combobox in your case) in the class constructor (before making the connection).
I gave you more alternatives, for you to choose what is more appropriate for you.

Like the blockSignals method looks more appropriate if you need to add some entry in combobox at run-time (if user selects/check something... the combobox might have more/less entries).

Anyway this is just my opinion, you know the design of the rest of your application, and you choose what fits best.

ShamusVW
12th August 2010, 06:43
Yes, thank you for all your advise.
You got me going on the right track.

What I have now done, is not disconnect/connect/block/unblock the signal at all.
I have set my initial connection in the constructor using

SIGNAL(activated(QString))

which responds only to user interaction. Previously I used

SIGNAL(currentIndexChanged(QString))

which responds to the combobox changing programmatically as well, i.e. populating it, which I didn't want.