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 sorting the items.
Forum Updated to NodeBB v4.3 + New Features

QComboBox sorting the items.

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 9.8k 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.
  • S Offline
    S Offline
    Sebastian
    wrote on last edited by Sebastian
    #1

    Dear All,

    I have to sort the QComboBox added items.
    Suppose the function rNo() is called from a loop and it filled the combo.

    void rNo( int num)
    {
    QComboBox *m_itemCombo;
    Example :
    itemCombo->addItem(num);
    }
    

    After execution of the function in combo, we have 5 items( 1,4,3,5,2). and it is not sorted.
    So is there any way like we sort the combo item after filling it?
    So my output be like in Combo is (1,2,3,4,5).
    Might be a silly question but is there anything like that Qt Supports?

    TIA

    JonBJ 1 Reply Last reply
    0
    • S Sebastian

      Dear All,

      I have to sort the QComboBox added items.
      Suppose the function rNo() is called from a loop and it filled the combo.

      void rNo( int num)
      {
      QComboBox *m_itemCombo;
      Example :
      itemCombo->addItem(num);
      }
      

      After execution of the function in combo, we have 5 items( 1,4,3,5,2). and it is not sorted.
      So is there any way like we sort the combo item after filling it?
      So my output be like in Combo is (1,2,3,4,5).
      Might be a silly question but is there anything like that Qt Supports?

      TIA

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Sebastian
      Either sort your items before adding them in the first place, or put them into a model like QStringList and sort that, or (the "proper Qt way") interpose a QSortFilterProxyModel between your model and the QComboBox. https://stackoverflow.com/questions/30957002/update-and-sort-qt-comboboxes-alphabetically gives various examples (a bit more complex than yours, so your solution will be smaller/simpler). Actually https://www.qtcentre.org/threads/3741-How-to-sort-a-QComboBox-in-Qt4?p=20187#post20187 from 2006 looks the simplest and I think would still be just the same now!

      1 Reply Last reply
      2
      • A.A.SEZENA Offline
        A.A.SEZENA Offline
        A.A.SEZEN
        wrote on last edited by
        #3
        comboBox->model()->sort(0, Qt::DescendingOrder); // default Qt::AscendingOrder
        
        1 Reply Last reply
        5

        • Login

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