Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] QComboBox duplicates
Forum Updated to NodeBB v4.3 + New Features

[Solved] QComboBox duplicates

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 11.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    annatz
    wrote on 9 Feb 2011, 11:25 last edited by
    #1

    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]

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on 9 Feb 2011, 11:41 last edited by
      #2

      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.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on 9 Feb 2011, 12:05 last edited by
        #3

        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);
        }
        }
        @

        1 Reply Last reply
        0
        • A Offline
          A Offline
          annatz
          wrote on 9 Feb 2011, 12:38 last edited by
          #4

          Now I understand..
          It works.

          thank you so much!

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on 10 Feb 2011, 20:59 last edited by
            #5

            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.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0

            1/5

            9 Feb 2011, 11:25

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved