PDA

View Full Version : avoid duplicates items in comboBox



mickey
12th April 2006, 20:42
Hi, I'm trying to avoid insert duplicates in a combox; insert item is called many times..
But don't works; every times isertItem() is called, it adds the same item in the comboBox.Why?


comboBox->setDuplicatesEnabled(FALSE); //this is in mainForm constructor
void MainForm::insertItem() {
(checkboxi[1]->isChecked()) ? comboBox->insertItem("item2",2) :
comboBox->removeItem(2);
}

jacek
12th April 2006, 21:07
Why?
Because you haven't read the documentation:
bool duplicatesEnabled
This property holds whether duplicates are allowed.
If the combobox is editable and the user enters some text in the combobox's lineedit and presses Enter (and the insertionPolicy() is not NoInsertion), then what happens is this:
If the text is not already in the list, the text is inserted.
If the text is in the list and this property is TRUE (the default), the text is inserted.
If the text is in the list and this property is FALSE, the text is not inserted; instead the item which has matching text becomes the current item.
This property only affects user-interaction. You can use insertItem() to insert duplicates if you wish regardless of this setting.
Set this property's value with setDuplicatesEnabled() and get this property's value with duplicatesEnabled().

mickey
12th April 2006, 22:13
OK. Then: I need insert and remove element in comboBox; to do this avoiding dupicate, do I implement the mechanism? Oe is there an istruction? (I don't find it) .Thanks

jacek
12th April 2006, 22:22
You will have to implement this yourself.

Brandybuck
13th April 2006, 04:44
Or simply check that the item isn't there before inserting it.