[Solved] QComboBox duplicates
-
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]
-
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.
-
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);
}
}
@ -
Now I understand..
It works.thank you so much!
-
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.