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. [C++] QComboBox insertPolicy doesn't work

[C++] QComboBox insertPolicy doesn't work

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 4.5k Views
  • 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.
  • G Offline
    G Offline
    Gugelhupf
    wrote on 28 Oct 2015, 22:09 last edited by
    #1

    Hi,

    I have created a QComboBox and set its "insertPolicy" to "QComboBox::InsertAlphabetically" (I can see with qDebug that it's "InsertAlphabetically" after setting it), but when I add a new item it always appears at the combo box bottom, the represented order in combo box seems to be "InsertAtBottom".

    Why doesn't InsertAlphabetically work when I add new items ? What should I do for making it work ?

    Thanks.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 28 Oct 2015, 22:12 last edited by
      #2

      Hi,

      Can you post a sample of how you initialize and insert data in your QComboBox ?

      By the way, which version of Qt are you using ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      G 1 Reply Last reply 28 Oct 2015, 22:22
      0
      • S SGaist
        28 Oct 2015, 22:12

        Hi,

        Can you post a sample of how you initialize and insert data in your QComboBox ?

        By the way, which version of Qt are you using ?

        G Offline
        G Offline
        Gugelhupf
        wrote on 28 Oct 2015, 22:22 last edited by Gugelhupf
        #3

        Hi @SGaist,

        I use Qt 5.5. First I initialize my QComboBox in my contructor :
        MyClass::MyClass(QWidget* parent) :
        QWidget(parent),
        myComboBox{new QComboBox(this)}
        {
        myComboBox->setInsertPolicy(QComboBox::InsertAlphabetically);
        qDebug() << myComboBox->insertPolicy(); // For checking policy
        }

        Then for adding new items :
        myComboBox->addItem(aNewQString);

        Thanks.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Gugelhupf
          wrote on 29 Oct 2015, 14:23 last edited by
          #4

          Hi everyone,

          I think it's maybe a bug. I have created a little test :

          #include <QComboBox>
          
          class ComboBoxReorderingTests
          {
          public:
              ComboBoxReorderingTests();
          
              static void test1_ComboBox(){
                  QComboBox* comboBox = new QComboBox;
                  comboBox->setInsertPolicy(QComboBox::InsertAlphabetically);
                  comboBox->show();
          
                  comboBox->addItem("A");
                  comboBox->addItem("C");
                  comboBox->addItem("B");
              }
          
              static void test2_ComboBox(){
                  QComboBox* comboBox = new QComboBox;
                  comboBox->setInsertPolicy(QComboBox::InsertAlphabetically);
                  comboBox->show();
          
                  QList<QString> list;
                  list << "A" << "C" << "B";
                  comboBox->addItems(list);
              }
          };
          
          int main(int argc, char *argv[]) {
              QApplication a(argc, argv);
              ComboBoxReorderingTests::test2_ComboBox(); // Already tested with test1_ComboBox() before
              return a.exec();
          }
          

          Expected : A, B, C
          test1 and test2 methods show me the wrong order : A, C, B

          Am I doing something wrong or is this a bug ? Is there any alternative solution without uninstalling Qt5 ? Thanks

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 29 Oct 2015, 14:54 last edited by
            #5

            After a quick check in the documentation I saw the error: the InsertPolicy is applied to User entered string no to programmatically added strings.

            If you want to ensure your string list is sorted before inserting them, sort the list (by the way you can use QStringList)

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            G 1 Reply Last reply 29 Oct 2015, 16:43
            1
            • S SGaist
              29 Oct 2015, 14:54

              After a quick check in the documentation I saw the error: the InsertPolicy is applied to User entered string no to programmatically added strings.

              If you want to ensure your string list is sorted before inserting them, sort the list (by the way you can use QStringList)

              G Offline
              G Offline
              Gugelhupf
              wrote on 29 Oct 2015, 16:43 last edited by
              #6

              Thanks @SGaist ,
              I didn't see it in the documentation.

              I have created my own function for it :

                  void comboboxAlphabeticalOrder(QComboBox* combobox){
                      int comboBoxCount = combobox->count();
              
                      if(comboBoxCount < 2){
                          return;
                      }
              
                      QStringList tempList;
              
                      for(int i = comboBoxCount - 1; i != -1; i--){
                          tempList << combobox->itemText(i);
                          combobox->removeItem(i);
                      }
              
                      tempList.sort();
                      combobox->addItems(tempList);
                  }
              
              J 1 Reply Last reply 20 Dec 2018, 10:53
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 29 Oct 2015, 20:36 last edited by
                #7

                You're welcome !

                Since you have it working now, please mark the thread as solved using the "Topic Tool" button so other forum users may know a solution has been found :)

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • G Gugelhupf
                  29 Oct 2015, 16:43

                  Thanks @SGaist ,
                  I didn't see it in the documentation.

                  I have created my own function for it :

                      void comboboxAlphabeticalOrder(QComboBox* combobox){
                          int comboBoxCount = combobox->count();
                  
                          if(comboBoxCount < 2){
                              return;
                          }
                  
                          QStringList tempList;
                  
                          for(int i = comboBoxCount - 1; i != -1; i--){
                              tempList << combobox->itemText(i);
                              combobox->removeItem(i);
                          }
                  
                          tempList.sort();
                          combobox->addItems(tempList);
                      }
                  
                  J Offline
                  J Offline
                  joe_zeth
                  wrote on 20 Dec 2018, 10:53 last edited by
                  #8

                  @Gugelhupf

                  This is great, thanks. the "myComboBox->setInsertPolicy(QComboBox::InsertAlphabetically" didnt seem to work for me either.

                  Also found the "remove item" can cause issues, adding in empty items. Best to clear the whole list to ensure nothing is in there at all, before re-adding to the box.
                  additionally, it is noteworthy this sort() function is CASE SENSITIVE, which will cause issues if not adhered to.

                  If you are finding the same issue try this.

                  void ReadDICOMSeriesQt::sort(QComboBox* combobox)
                  {
                  	int comboBoxCount = combobox->count();
                  
                  	if (comboBoxCount < 2) {
                  		return;
                  	}
                  
                  	QStringList tempList;
                  
                  	for (int i = 0; i < comboBoxCount; i++) {
                  		tempList << combobox->itemText(i);
                  	}
                  	combobox->clear();
                  
                  	tempList.sort();
                  	combobox->addItems(tempList);
                  
                  	//check list
                  	//for (int k = 0; k < tempList.count(); k++)
                  	//{
                     //cout << tempList.at(k).toStdString() << endl;
                  	//}
                  
                  }
                  
                  1 Reply Last reply
                  0

                  • Login

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