Results 1 to 4 of 4

Thread: Using QSortFilterProxyModel

  1. #1
    Join Date
    Oct 2007
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Using QSortFilterProxyModel

    Hi,

    I need some help with the use of the QSortFilterProxyModel. I have searched the documentation and here on the forum but haven't been able to find any similar issues.

    This is what I want to do:

    I have two table views that should visualize the same data. One is a filtered version of the other so it will not show all columns. I call them collapsedView and expandedView. Both are QTableView.

    To accomplish this filtering functionality I have added a QSortFilterProxyModel between the sourceModel (of type QAbstractTableModel) and the collapsedView:

    Qt Code:
    1. QTableView* collapsedView = new QTableView();
    2. QTableView* expandedView = new QTableView();
    3.  
    4. SourceModel* sourceModel = new SourceModel(); // SourceModel is derived from QAbstractTableModel
    5. FilterModel* filterModel = new FilterModel(); // FilterModel is derived from QSortFilterProxyModel
    6. filterModel->setSourceModel(sourceModel);
    7.  
    8. collapsedView->setModel(filterModel);
    9. expandedView->setModel(sourceModel);
    To copy to clipboard, switch view to plain text mode 

    This works fine and both the views behave as I want.
    But I also want to be able to sort the tables by clicking on the column headers. And I also want both the views to be sorted the same way regardless in what view I click the header. If I sort the items in one view, the other view should rearrange its rows in the same way.

    So I tried to add another QSortFilterProxyModel between the source model and the filter model that should handle the sorting for both the views:

    Qt Code:
    1. SourceModel* sourceModel = new SourceModel();
    2. SortModel* sortModel = new SortModel(); // SortModel is derived from QSortFilterProxyModel
    3. sortModel->setSourceModel(sourceModel);
    4. FilterModel* filterModel = new FilterModel();
    5. filterModel->setSourceModel(sortModel);
    6.  
    7. collapsedView->setModel(filterModel);
    8. expandedView->setModel(sortModel);
    To copy to clipboard, switch view to plain text mode 

    I also reimplemented the lessThan-method in the sortModel.
    I am now able to sort according to columns by clicking on the header, but only the view that I click in are sorted. When I switch to the other view, this one is not affected by the sort.
    It seems that the sorting only affects the view that the sorting was called from. How can I get the other view to be aware that it should change it layout?
    Tried to emit the layoutChanged signal, but it doesn't seem to help.

    Is this the right approach? Should I implement the sort-function on the source model instead? Thought I wouldn't have to do that since there is this proxy model for sorting.

    I'd be grateful for any input!

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

    Default Re: Using QSortFilterProxyModel

    Quote Originally Posted by Jennie Bystrom View Post
    I have two table views that should visualize the same data. One is a filtered version of the other so it will not show all columns. I call them collapsedView and expandedView. Both are QTableView.
    You don't need a proxy model to hide columns if that's all difference between them. You can use QTableView::setColumnHidden().

    Qt Code:
    1. QTableView* collapsedView = new QTableView();
    2. QTableView* expandedView = new QTableView();
    3.  
    4. SourceModel* sourceModel = new SourceModel();
    5. FilterModel* filterModel = new FilterModel();
    6. filterModel->setSourceModel(sourceModel);
    7.  
    8. collapsedView->setModel(filterModel);
    9. expandedView->setModel(filterModel);
    10.  
    11. collapsedView->setColumnHidden(2, true); // hide for example second column
    To copy to clipboard, switch view to plain text mode 
    This way sorting should work correctly out of the box in both views.
    J-P Nurmi

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

    Jennie Bystrom (6th December 2007)

  4. #3
    Join Date
    Oct 2007
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using QSortFilterProxyModel

    Ah, what a simple solution to my problem!
    Thanks a lot!

    Just for curiosity, how would you go about do this if the filter was a bit more advanced and you would have to use the proxy model for the filter? As far as I know, this is all I need for now, but that may change in the future.

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

    Default Re: Using QSortFilterProxyModel

    I guess in that case you would have to connect to both table's horizontal header's signal QHeaderView::clicked() and apply the corresponding sorting column and order to the other table.
    J-P Nurmi

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.