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. QComboBox deactivate automatic alphabetical sorting

QComboBox deactivate automatic alphabetical sorting

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.4k 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.
  • V Offline
    V Offline
    valchr_
    wrote on last edited by valchr_
    #1

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

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • V Offline
        V Offline
        valchr_
        wrote on last edited by
        #3
        • 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;");
        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • V Offline
            V Offline
            valchr_
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              3
              • V Offline
                V Offline
                valchr_
                wrote on last edited by
                #7

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

                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