QComboBox sorting the items.
-
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
-
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
@Sebastian
Either sort your items before adding them in the first place, or put them into a model likeQStringList
and sort that, or (the "proper Qt way") interpose aQSortFilterProxyModel
between your model and theQComboBox
. 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!