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.
QtWS25 Last Chance

QComboBox sorting the items.

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 9.5k 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 7 Jan 2020, 10:58 last edited by Sebastian 1 Jul 2020, 10:59
    #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

    J 1 Reply Last reply 7 Jan 2020, 11:06
    0
    • S Sebastian
      7 Jan 2020, 10:58

      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

      J Offline
      J Offline
      JonB
      wrote on 7 Jan 2020, 11:06 last edited by JonB 1 Jul 2020, 11:09
      #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 Offline
        A Offline
        A.A.SEZEN
        wrote on 7 Jan 2020, 11:13 last edited by
        #3
        comboBox->model()->sort(0, Qt::DescendingOrder); // default Qt::AscendingOrder
        
        1 Reply Last reply
        5

        3/3

        7 Jan 2020, 11:13

        • Login

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