[C++] QComboBox insertPolicy doesn't work



  • 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.


  • Lifetime Qt Champion

    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 ?



  • 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.



  • 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


  • Lifetime Qt Champion

    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)



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

  • Lifetime Qt Champion

    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 :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.