[Solved] QComboBox duplicates
-
wrote on 9 Feb 2011, 11:25 last edited by
hi,
I am trying to fill a combobox without duplicates.
i have created a qmap with some records of an xml file.The code is:
@QComboBox *kliniki = new QComboBox();
kliniki->setDuplicatesEnabled (false);
while(!persons.isEmpty()) {
QMap<QString,QString> clinic = persons.takeFirst();
kliniki->addItem(clinic["clinical_department"]);}
@The map has duplicates.
How can i disable them?[EDIT: code formatting, please use single @-tags before and after the code, Volker]
-
wrote on 9 Feb 2011, 11:41 last edited by
Read the docs:
bq. "QComboBox::setDuplicatesEnabled() ":http://doc.qt.nokia.com/4.7/qcombobox.html#duplicatesEnabled-prop
This property holds whether the user can enter duplicate items into the combobox.
Note that it is always possible to programmatically insert duplicate items into the combobox.
By default, this property is false (duplicates are not allowed).You must check yourself.
-
wrote on 9 Feb 2011, 12:05 last edited by
One way to get rid of duplicates, is to insert them in a QSet. Something like this would probably work:
@
QComboBox *kliniki = new QComboBox();
QSet<QString> uniqueSet;
while(!persons.isEmpty()) {
QMap<QString,QString> clinic = persons.takeFirst();
QString department = clinic.value("clinical_department");
if (!uniqueSet.contains(department)) {
kliniki->addItem(department);
uniqueSet.insert(department);
}
}
@ -
wrote on 9 Feb 2011, 12:38 last edited by
Now I understand..
It works.thank you so much!
-
wrote on 10 Feb 2011, 20:59 last edited by
You can also put the options for the ComboBox into a "QStringList":http://doc.qt.nokia.com/latest/qstringlist.html (and check duplicates there) and call "QComboBox::addItem() ":http://doc.qt.nokia.com/latest/qcombobox.html#addItems to insert them all at once.
1/5