Results 1 to 5 of 5

Thread: Qt4.1.1: Sorting QCombobox entries

  1. #1
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Qt4.1.1: Sorting QCombobox entries

    With Qt3, you could use combo->listbox()->sort()

    Is there a simple way to do this using Qt4 now that the standard listbox is provided by a QListView instead of a QListBox?

  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: Qt4.1.1: Sorting QCombobox entries

    Mmmh.. interesting!

    It seems that a combo with a "model" sorts itself fine, but a combo with "items" does not.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6.  
    7. QStringList items;
    8. for (int i = 9; i >= 0; --i)
    9. items << QString::number(i);
    10.  
    11. QComboBox modelCombo;
    12. modelCombo.setWindowTitle("Model");
    13. modelCombo.setModel(new QStringListModel(items));
    14. modelCombo.model()->sort(0);
    15. modelCombo.show();
    16.  
    17. QComboBox itemCombo;
    18. itemCombo.setWindowTitle("Item");
    19. itemCombo.addItems(items);
    20. itemCombo.model()->sort(0);
    21. itemCombo.show();
    22.  
    23. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    24. return a.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

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

    Byngl (15th March 2006)

  4. #3
    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: Qt4.1.1: Sorting QCombobox entries

    Btw, the only entry (related even a bit to this) I found in the task tracker.

  5. #4
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Qt4.1.1: Sorting QCombobox entries

    OK, I can force the model to be a QStringListModel so I can sort it, but if I do, I seem to be losing the ability to assign data to the entries.

    Qt Code:
    1. //
    2. QComboBox itemCombo;
    3. itemCombo.setWindowTitle("Item");
    4. for (int i = 9; i >= 0; --i)
    5. {
    6. itemCombo.addItem(QString::number(i), 100 + i);
    7. }
    8. for (int i = 0; i < itemCombo.count(); ++i)
    9. {
    10. cout << i << ' ' << itemCombo.itemText(i).toLatin1().data() << ' ';
    11. cout << itemCombo.itemData(i).toInt();
    12. cout << " [" << itemCombo.itemData(i).toString().toLatin1().data() << ']';
    13. cout << endl;
    14. }
    15.  
    16. //
    17. // Clear the contents and try again, this time with
    18. // a non-default model
    19. //
    20. itemCombo.clear();
    21. itemCombo.setModel(new QStringListModel());
    22. for (int i = 9; i >= 0; --i)
    23. {
    24. itemCombo.addItem(QString::number(i), 100 + i);
    25. }
    26. cout << "==============" << endl;
    27. for (int i = 0; i < itemCombo.count(); ++i)
    28. {
    29. cout << i << ' ' << itemCombo.itemText(i).toLatin1().data() << ' ';
    30. cout << itemCombo.itemData(i).toInt();
    31. cout << " [" << itemCombo.itemData(i).toString().toLatin1().data() << ']';
    32. cout << endl;
    33. }
    34.  
    35. itemCombo.model()->sort(0);
    36. itemCombo.show();
    To copy to clipboard, switch view to plain text mode 
    This produces the following output:
    Qt Code:
    1. 0 9 109 [109]
    2. 1 8 108 [108]
    3. 2 7 107 [107]
    4. 3 6 106 [106]
    5. 4 5 105 [105]
    6. 5 4 104 [104]
    7. 6 3 103 [103]
    8. 7 2 102 [102]
    9. 8 1 101 [101]
    10. 9 0 100 [100]
    11. ==============
    12. 0 9 0 []
    13. 1 8 0 []
    14. 2 7 0 []
    15. 3 6 0 []
    16. 4 5 0 []
    17. 5 4 0 []
    18. 6 3 0 []
    19. 7 2 0 []
    20. 8 1 0 []
    21. 9 0 0 []
    To copy to clipboard, switch view to plain text mode 
    I just checked in qstringlistmodel.cpp and it only returns data for Qt::DisplayRole and Qt::EditRole, not Qt::UserRole. Guess I get to write my own extension...
    Last edited by Byngl; 15th March 2006 at 23:46.

  6. #5
    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: Qt4.1.1: Sorting QCombobox entries

    I guess an easy solution could be to inherit QStandardItemModel (which QComboBox internally uses) and implement the sorting functionality..

Similar Threads

  1. Editing and adding entries to a QComboBox
    By bruccutler in forum Newbie
    Replies: 1
    Last Post: 29th May 2007, 17:13

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.