Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QComboBox deactivate automatic alphabetical sorting



  • Hey everyone !

    I'm using a QComboBox like that :

    while (query.next)
    {
      this->ui->comboBoxChoixTest->insertItem(0,query.value(0).toString());
    }
    query.prepare("COMMIT");
    query.exec();
    
    this->ui->comboBoxChoixTest->insertItem(0,"Choisir un Test");
    this->ui->comboBoxChoixTest->setCurrentText("Choisir un Test");
    

    I insert all items to index 0 so they have to stay in the order that I added them.

    BUT actually it seems like there is an automatic alphabetical sorting…

    Is it possible to avoid this sorting ?

    Thanks :)


  • Lifetime Qt Champion

    The default insert policy is not set to alphabetic sorting so I assume you enabled it (e.g. in the designer) - therefore please provide a minimal, compilable example to reproduce the issue.



    • Insert Policy is set to insertAtTop in Designer.
    • By the way I use a stylesheet : QApplication::setStyle(QStyleFactory::create("Fusion"));
    • And also this style for the ComboBox : this->ui->comboBoxChoixTest->setStyleSheet("combobox-popup: 0;");

  • Lifetime Qt Champion

    Please provide an example with your problem, this works fine for me:

    int main(int argc, char **argv)
    {
      QApplication app(argc, argv);
      QComboBox cbx;
      cbx.insertItem(0, "A");
      cbx.insertItem(0, "B");
      cbx.show();
      return app.exec();
    }
    


  • Oh I found Something ! It comes from my queries…

    query.prepare("BEGIN TRANSACTION");
    	query.exec();
    	query.prepare("SELECT Nom_Test FROM LISTE_TESTS");
    	query.exec();
    	while (query.next())
    	{
    		cout << query.value(0).toString().toStdString() << endl;
    		this->ui->comboBoxChoixTest->insertItem(0,query.value(0).toString());
    	}
    	query.prepare("COMMIT");
    	query.exec();
    

    My table "LISTE_TESTS" is in this order :

    -TU1
    -TU2
    -TU0
    -ok
    -tu1
    -tu0
    -tu2

    But with the cout it return like that (alphabeticaly) :

    TU0
    TU1
    TU2
    ok
    tu0
    tu1
    tu2

    Okay so the problem comes from the queries but I can't see what is it...


  • Lifetime Qt Champion

    That's the reason why I wanted a minimal example from you...



  • Okay I used an ORDER BY query and now it's good ! SOLVED


Log in to reply