Results 1 to 6 of 6

Thread: QSortFilterProxyModel and QComboBox

  1. #1
    Join Date
    Jan 2010
    Posts
    11
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Question QSortFilterProxyModel and QComboBox

    Hi,
    I want to sort a QComboBox locale aware, so I used a reimplementation of QSortFilterProxyModel. Unfortunately, it messes up the contents of the QComboBox if I insert Items after calling MyCombo->model()->sort(0);, which does not happen if I just use QSortFilterProxyModel without reimplementing it, so I guess my reimplementation is "naive" and something is missing/wrong.

    In the example (stripped to the important stuff, but is compilable) I add A, C, B to the ComboBox, sort it, then I add D and the ComboBox shows <empty line>, A, B, D - and C is missing. Has anybody an idea what I did wrong reimplemnting QSortFilterProxyModel?

    Thanks for your help
    Armin

    Files: (attached also as zip)

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    mysortfilterproxymodel.h
    Qt Code:
    1. #ifndef MYSORTFILTERPROXYMODEL_H
    2. #define MYSORTFILTERPROXYMODEL_H
    3.  
    4. #include <QtGUI>
    5.  
    6. class MySortFilterProxyModel : public QSortFilterProxyModel
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. MySortFilterProxyModel(QObject *parent);
    12. bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
    13. };
    14. #endif // MYSORTFILTERPROXYMODEL_H
    To copy to clipboard, switch view to plain text mode 

    mysortfilterproxymodel.cpp
    Qt Code:
    1. #include "mysortfilterproxymodel.h"
    2.  
    3. MySortFilterProxyModel::MySortFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent)
    4. {
    5. }
    6. //---------------------------------------------------------------------------
    7. bool MySortFilterProxyModel::lessThan(const QModelIndex &left,
    8. const QModelIndex &right) const
    9. {
    10. QVariant leftData = sourceModel()->data(left);
    11. QVariant rightData = sourceModel()->data(right);
    12.  
    13. QString leftString = leftData.toString();
    14. QString rightString = rightData.toString();
    15.  
    16. return QString::localeAwareCompare(leftString, rightString) < 0;
    17. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGUI>
    5. #include "mysortfilterproxymodel.h"
    6.  
    7. class MainWindow : public QMainWindow {
    8. Q_OBJECT
    9. public:
    10. MainWindow(QWidget *parent = 0);
    11. ~MainWindow();
    12.  
    13. QComboBox *MyCombo;
    14. MySortFilterProxyModel *MyProxySortModel;
    15.  
    16. };
    17. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    4. {
    5. MyCombo = new QComboBox(this);
    6. MySortFilterProxyModel *MyProxySortModel = new MySortFilterProxyModel(MyCombo);
    7. MyProxySortModel->setSourceModel(MyCombo->model());
    8. MyCombo->model()->setParent(MyProxySortModel);
    9. MyCombo->setModel(MyProxySortModel);
    10.  
    11. MyCombo->addItem("A", 1);
    12. MyCombo->addItem("C", 3);
    13. MyCombo->addItem("B", 2);
    14.  
    15. MyCombo->model()->sort(0);
    16.  
    17. MyCombo->addItem("D", 4);
    18. }
    19. MainWindow::~MainWindow() {}
    To copy to clipboard, switch view to plain text mode 

    comboproxy.zip
    Last edited by Armin; 21st February 2010 at 10:02. Reason: typo

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel and QComboBox

    Sorry Armin after further testing I discovered my suggestion didn't work.
    Last edited by norobro; 21st February 2010 at 21:13.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSortFilterProxyModel and QComboBox

    Some point I can add is, that with calling MyCombo->setModel(MyProxySortModel); the "sourceModel()" is deleted:
    Qt Code:
    1. void QComboBox::setModel(QAbstractItemModel *model)
    2. {
    3. Q_D(QComboBox);
    4. //...
    5. if (d->model->QObject::parent() == this)
    6. delete d->model;
    To copy to clipboard, switch view to plain text mode 
    But even if you call a MyCombo->model()->setParent(this); before the problem is not solved...

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel and QComboBox

    I found an old thread that states that this was fixed in Qt 4.2. Hmmmmm

    I got it to work by using
    Qt Code:
    1. MyCombo->insertItem(0,"D", 4);
    To copy to clipboard, switch view to plain text mode 
    Of course this puts the "D" first so you have to resort. I also tried the following to put the item at the end of the list but got the same result as "addItem()"
    Qt Code:
    1. MyCombo->insertItem(MyCombo->count(),"D", 4);
    To copy to clipboard, switch view to plain text mode 
    Last edited by norobro; 21st February 2010 at 23:09. Reason: typo

  5. The following user says thank you to norobro for this useful post:

    Armin (22nd February 2010)

  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSortFilterProxyModel and QComboBox

    Quote Originally Posted by norobro View Post
    I got it to work by using
    Qt Code:
    1. MyCombo->insertItem(0,"D", 4);
    To copy to clipboard, switch view to plain text mode 
    Of course this puts the "D" first so you have to resort.
    Instead of manually resort you can try QSortFilterProxyModel::setDynamicSortFilter().

  7. The following user says thank you to Lykurg for this useful post:

    Armin (22nd February 2010)

  8. #6
    Join Date
    Jan 2010
    Posts
    11
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSortFilterProxyModel and QComboBox

    Hi norobro & Lykurg,

    many thanks for your ideas, insertItem() instead of addItem() works fine, and dynamic sorting makes it easier. Do you agree that there must be some bug with addItem()? Hope that I can manage all other things which will come up in my little "Qt begiunners project" by myself without taking your time. Sorting things in Qt was/is really a challenge for me :-).

    Have a nice day
    Armin

Similar Threads

  1. Problems with QSortFilterProxyModel
    By Nefastious in forum Newbie
    Replies: 10
    Last Post: 31st October 2009, 18:51
  2. QSortFilterProxyModel with QComboBox
    By h123 in forum Qt Programming
    Replies: 1
    Last Post: 10th June 2009, 19:16
  3. QSortFilterProxyModel - crash
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 4th June 2008, 14:14
  4. Using QSortFilterProxyModel
    By Jennie Bystrom in forum Qt Programming
    Replies: 3
    Last Post: 6th December 2007, 10:28
  5. QSortFilterProxyModel
    By evgenM in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2007, 11:53

Tags for this Thread

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.