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. #2
    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

    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

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

    guenthk (21st 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.