×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Regular Contributor
    Join Date
    Nov 2006
    Posts
    63

    QSortFilterProxyModel search problem

    Hi yall,
    so I'm trying to do a mulltiple-field sort (i.e. first by surname then by name) and I've chosen to subclass the QSortFilterProxyModel.
    I've subclassed it and reimplemented functions
    Code:
        virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
        virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
    Then my model is derived from a QAbstractListModel (I want to just keep the reference to a list being kept elsewhere)
    so I reimplemented functions
    Code:
        //basic function for a read-only model
        int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
        QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const ;
    Then all this content is controlled in a custom list (the purpose is to add i.e. the kinetic scrolling with Flickable)
    Code:
        class MyList : public QListView
    and displayed in a custom delegate
    Code:
        class MyDelegate : public QItemDelegate
    In the constructor of the widget that holds that list (it's a central Widget) I do:
    Code:
        m_listview = new MyList(this);
        m_listview->setStyle(new QWindowsStyle);
        m_listview->setAutoFillBackground(true);
    
        MyModel *model = new MyModel (dataArray, this);
    
        MySortProxyModel *proxy = new MySortProxyModel(this);
        proxy->setDynamicSortFilter(true);
        proxy->setSourceModel(model);
        m_listview->setModel(proxy);
    
        MyDelegate *delegate = new MyDelegate(this);
        m_listview->setItemDelegate(delegate);
    Now the problem is that the list is not sorted, and I've debugged it to see that the function filterAcceptsRow is called but lessThan NOT!
    I've tried to read the Qt source, but not much luck there as I get lost when some signal is fired.
    Please help, I wonder where I missed some decoration to make it work properly...
    Krzysztof Kucharewicz

  2. #2
    Regular Contributor
    Join Date
    Nov 2006
    Posts
    63

    Re: QSortFilterProxyModel search problem

    apparently it was the QListView that didn't have the sort method implemented, so I had to call the sort method manually after assigning the model.

    Code:
        m_listview->setModel(proxy);
        m_listview->model()->sort(0);
    Krzysztof Kucharewicz

  3. #3
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QSortFilterProxyModel search problem

    Note that you are calling sort method of a model, so problem is not connected to QListView in any imaginable way.

  4. #4
    Regular Contributor
    Join Date
    Nov 2006
    Posts
    63

    Re: QSortFilterProxyModel search problem

    Well, the filtering and sorting happens when the model is being assigned to the view, so I guess it is connected to the view in some way?
    As I compared the functionality of the QListView with i.e. QTreeView, I've noticed there is a function
    Code:
    void sortByColumn(int column, Qt::SortOrder order);
    in the latter. This was my guess that maybe it's because of QListView.


    On the other hand the example from chapter 4 of Beginning Nokia Apps Development: Qt actually uses QListView. The difference is the model that inherits the QStandardItemModel in the example instead of my QAbstractListModel.

    Any clue where I could have missed something?
    Krzysztof Kucharewicz

  5. #5
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QSortFilterProxyModel search problem

    Quote Originally Posted by krix View Post
    Well, the filtering and sorting happens when the model is being assigned to the view
    Sort happens when you call sort method of the model.
    http://doc.trolltech.com/4.7/qabstra...odel.html#sort

  6. #6
    Regular Contributor
    Join Date
    Nov 2006
    Posts
    63

    Re: QSortFilterProxyModel search problem

    again, by calling
    Code:
    proxy->setDynamicSortFilter(true);
    I expect the model to be sorted without manually calling the sort method
    Krzysztof Kucharewicz

  7. #7
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QSortFilterProxyModel search problem

    This property holds whether the proxy model is dynamically sorted and filtered whenever the contents of the source model change.

  8. #8
    Regular Contributor
    Join Date
    Nov 2006
    Posts
    63

    Re: QSortFilterProxyModel search problem

    The contents were filtered automatically but not sorted

    Quote Originally Posted by krix View Post
    Now the problem is that the list is not sorted, and I've debugged it to see that the function filterAcceptsRow is called but lessThan NOT!
    Krzysztof Kucharewicz

Similar Threads

  1. Replies: 3
    Last Post: 2011-01-21, 13:59
  2. QSortFilterProxyModel question
    By Olorin5800 in forum Qt
    Replies: 5
    Last Post: 2010-09-20, 12:45
  3. Possible to do service search without device search?
    By ed_welch in forum Mobile Java Networking & Messaging & Security
    Replies: 8
    Last Post: 2008-02-11, 11:35

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×