Populating, signals and comboboxes
Hello
I have a signal/slot connected to a combobox
Quote:
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.
Re: Populating, signals and comboboxes
Try disconnect(qcombobox...), then populate, and connect(qcombobox...) again.
Or use QStandardItemModel, add items to the model and setModel on the qcombobox.
Re: Populating, signals and comboboxes
You can create the connection only after the combobox is populated, or you can block signals or disconnect.
But my advice is to do the first (populate before connection), it's also a good practice to initialize variables as soon as possible.
Re: Populating, signals and comboboxes
Thanks. Did it the first way, works well.
Re: Populating, signals and comboboxes
Actually, will do
comboBox->blockSignals(true)
then populate, then
comboBox->blockSignals(false)
Seems more elegant!
Re: Populating, signals and comboboxes
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.
Re: Populating, signals and comboboxes
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.