Results 1 to 8 of 8

Thread: How to sort a QComboBox in Qt4

  1. #1
    Join Date
    Sep 2006
    Location
    Seeheim/Germany
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question How to sort a QComboBox in Qt4

    In Qt3 you could access the list box of a combo box and easily sort its items by calling the sort method of the list box.

    In Qt4 you can get the abstract item model of a combo box, but the implementation of the sort method of QAbstractItemModel does nothing.

    Is there a similarly simple way as in Qt3 to sort the items of a QComboBox in Qt4 after the combo box has been initialized by inserting items in any order?

    (I have seen thread http://www.qtcentre.org/forum/showth...=sorting+combo, but it doesn't fully answer my question.)

    Many thanks in advance,

    Klaus.

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

    Default Re: How to sort a QComboBox in Qt4

    Qt Code:
    1. combo->addItems(QStringList() << "B" << "C" << "A"); // "B","C","A"
    2. // for sorting you need the following 4 lines
    3. QSortFilterProxyModel* proxy = new QSortFilterProxyModel(combo); // <--
    4. proxy->setSourceModel(combo->model()); // <--
    5. // combo's current model must be reparented,
    6. // otherwise QComboBox::setModel() will delete it
    7. combo->model()->setParent(proxy); // <--
    8. combo->setModel(proxy); // <--
    9. // sort
    10. combo->model()->sort(0); // "A","B","C"
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    guenthk (21st September 2006)

  4. #3
    Join Date
    Sep 2006
    Location
    Seeheim/Germany
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to sort a QComboBox in Qt4

    Many thanks for your immediate reply.

    Unfortunately the proposed solution doesn't fully work since our combo box comprises user data, and the latter seems to disappear when applying your proposal.

    Do you know an easy way how to keep it?

    Klaus.

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

    Default Re: How to sort a QComboBox in Qt4

    It does? QComboBox uses a QStandardItemModel for storing it's items so I don't see any reason why wouldn't a QSortFilterProxyModel work in between.

    Qt Code:
    1. // add items with user data
    2. combo->addItem("B", "B's data");
    3. combo->addItem("C", "C's data");
    4. combo->addItem("A", "A's data");
    5.  
    6. // sort filter proxy model has been created and set like in the earlier post..
    7.  
    8. for (int i = 0; i < combo->count(); ++i)
    9. qDebug() << combo->itemText(i) << combo->itemData(i).toString();
    10. // outputs before sorting:
    11. // "B" "B's data"
    12. // "C" "C's data"
    13. // "A" "A's data"
    14.  
    15. // sort
    16. combo->model()->sort(0);
    17.  
    18. for (int i = 0; i < combo->count(); ++i)
    19. qDebug() << combo->itemText(i) << combo->itemData(i).toString();
    20. // outputs after sorting:
    21. // "A" "A's data"
    22. // "B" "B's data"
    23. // "C" "C's data"
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    guenthk (21st September 2006)

  7. #5
    Join Date
    Sep 2006
    Location
    Seeheim/Germany
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to sort a QComboBox in Qt4

    I haven't found out so far, why the user data (which is a pointer to a user-defined class) disappears (i.e., access to itemData yields a "0" pointer).

    But I have another problem with your proposal:

    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.

    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.

    I have tried this out, but I get a crash which seems to be caused by the fact that the QSortFilterProxyModel doesn't have a persistent model index. Again I don't know the reason for that, nor how I could enforce the construction of a persistent model index. But in principle I believe that the construction of an intermediate QSortFilterProxyModel is what TrollTech has in mind as the proper solution of the sorting problem.

    I have posted the issue also to the TrollTech bug/task tracker, but I am uncertain if they will give me a satisfactory answer.

    Klaus.

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

    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

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

    guenthk (25th September 2006)

  10. #7
    Join Date
    Sep 2006
    Location
    Seeheim/Germany
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post Re: How to sort a QComboBox in Qt4

    Many thanks again for your effort.

    I think we should try to do without the Qt4 sorting support and write a sorted insert operation for our combo boxes instead. (Trolltech has promised in their bug tracker to provide this feature in Qt 4.3).

    I think you should submit your funny sorting example to the Trolltech bug tracker, too. (Or shall I?)

    regards,

    Klaus.

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

    Default Re: How to sort a QComboBox in Qt4

    The trolls answered that this has already been fixed to Qt 4.2. I tested with 4.2.0-rc1 and no items disappeared indeed. However, the second sort doesn't work. Meaning that the final result is incorrect (A,B,C,F,E,D). Could someone with a recent snapshot of Qt 4.2 compiled test this before I bug them more?
    J-P Nurmi

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.