Results 1 to 8 of 8

Thread: How to sort a QComboBox in Qt4

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: How to sort a QComboBox in Qt4

    Quote Originally Posted by guenthk View Post
    The contents of our combo box is changed and re-sorted again and again, depending on user actions. According to your proposal, the combo box model would be replaced then on every sort operation while the original model would be retained, too. So we would get an increasing number of models this way.
    Well, sorry if I was unclear, but this is not what I meant. I meant that the proxy model would be applied only once.

    As I have understood the model/view philosophy of Qt4 and of QSortFilterProxyModel, the correct procedure as to sorting of list views is to insert an intermediate QSortFilterProxyModel between the base/source model and the view without replacing the source model, and then to sort the QSortFilterProxyModel.
    Yes, this is correct and also exactly the way I wanted it to work with combo box too..

    I noticed something funny. Sorting items between insertions makes items disappear, indeed. The sorting seems to work only once or something. Try uncommenting the first line doing the sort.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class SortableComboBox : public QComboBox
    4. {
    5. public:
    6. SortableComboBox(QWidget* parent = 0)
    7. : QComboBox(parent)
    8. {
    9. QStandardItemModel* model = new QStandardItemModel(0, 1, this);
    10. proxy->setSourceModel(model);
    11. setModel(proxy);
    12. }
    13. };
    14.  
    15. int main(int argc, char* argv[])
    16. {
    17. QApplication a(argc, argv);
    18. SortableComboBox combo;
    19.  
    20. combo.addItem("B");
    21. combo.addItem("C");
    22. combo.addItem("A");
    23.  
    24. // combo.model()->sort(0); // <- uncomment this line and items disappear..
    25.  
    26. combo.addItem("F");
    27. combo.addItem("E");
    28. combo.addItem("D");
    29.  
    30. combo.model()->sort(0);
    31.  
    32. combo.show();
    33. return a.exec();
    34. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  2. The following user says thank you to jpn for this useful post:

    guenthk (25th September 2006)

Similar Threads

  1. segmentation fault insert QString in QCombobox
    By regix in forum Qt Programming
    Replies: 16
    Last Post: 8th August 2006, 08:46
  2. Automatically add items to a QComboBox (Qt4)
    By darkadept in forum Qt Programming
    Replies: 2
    Last Post: 19th May 2006, 15:32
  3. QcomboBox items
    By therealjag in forum Newbie
    Replies: 5
    Last Post: 27th March 2006, 08:21
  4. QComboBox +SUSE10.0 +qt4.1 strange behavior
    By antonio.r.tome in forum Qt Programming
    Replies: 6
    Last Post: 20th March 2006, 17:49
  5. QComboBox inside QTableWidget
    By campana in forum Qt Programming
    Replies: 7
    Last Post: 20th March 2006, 17:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.