[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.
-
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,
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, BAm I doing something wrong or is this a bug ? Is there any alternative solution without uninstalling Qt5 ? Thanks
-
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)
-
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); }
-
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 :)
-
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); }
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; //} }