Important: Please read the Qt Code of Conduct -

[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();

    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() ":
    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)) {

  • Now I understand..
    It works.

    thank you so much!

  • You can also put the options for the ComboBox into a "QStringList": (and check duplicates there) and call "QComboBox::addItem() ": to insert them all at once.

Log in to reply