Results 1 to 4 of 4

Thread: benefits SortFilterProxyModel vs own sort in custom table model?

  1. #1
    Join Date
    Apr 2015
    Posts
    20
    Thanks
    7
    Qt products
    Platforms
    Unix/X11

    Default benefits SortFilterProxyModel vs own sort in custom table model?

    Got a custom QAbstractTableModel, and in it I implemented my own sort method based on column and order.

    its fine.

    But then I realized that the data are sorted only when user clicks column headers, not when table gets repopulated by new data...
    So I am fixing it now along with remembering sorting behaviour between restarts. So that if user clicked sorted by date its sorted by date next time application starts.

    But i also came over SortFilterProxyModel that I could implement. Is that the better way?

    The speed is my most important aspect, as my program is just a search in a database showing results in tableview as you type queries, and it needs to be instant.

    So not sure if one way or the other is more qualified.
    Last edited by DoTheEvo; 3rd November 2016 at 19:47. Reason: updated contents

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: benefits SortFilterProxyModel vs own sort in custom table model?

    QSortFilterProxyModel usually sits between your model and the view where the model data is displayed. Its purpose is to allow your app to display a sorted version of the model or to extract subsets of it without actually changing the underlying model. This is useful if, for example, you have two different views of the same model. When the model is updated, all views will be automatically updated.

    Your implementation confuses the model with the view of the model. Sorting at the view level (by a column click) is a local sort that doesn't change the model. You have propagated that back into the model.

    You can either implement a QSortFilterProxyModel, or have your view (or the widget that holds the view if you don't want to derive a new class from QTableView) connect a slot to one of the QAbstractItemModel signals that indicates a change (like modelReset()). In this slot, you can simply call QTableView::sortByColumn() to re-sort the new content. You are probably already connecting to the QHeaderView::sortIndicatorChanged() signal; just remember the arguments for future use when the model changes.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    DoTheEvo (3rd November 2016)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: benefits SortFilterProxyModel vs own sort in custom table model?

    Quote Originally Posted by DoTheEvo View Post
    But i also came over SortFilterProxyModel that I could implement. Is that the better way?
    That is a generic way, not necessarily better in ever aspect.

    It is "better" in the sense that you don't need to implement anything, i.e. avoiding a source of error/mistake.

    Your approach of providing a sorted view on the data is better as it can take the data, its inherent semantic and update behavior into account.

    Quote Originally Posted by DoTheEvo View Post
    The speed is my most important aspect, as my program is just a search in a database showing results in tableview as you type queries, and it needs to be instant.
    I would go with directly presenting the data in the way it should be displayed, i.e. have the model provide the data in the way it should be sorted.
    Either by sorting in the model or letting the database already retrieve the values that way.

    Cheers,
    _

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

    DoTheEvo (3rd August 2017)

  6. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: benefits SortFilterProxyModel vs own sort in custom table model?

    I would go with directly presenting the data in the way it should be displayed, i.e. have the model provide the data in the way it should be sorted.
    If speed is the most important aspect, I would agree with this. QSortFilterProxyModel or letting the view sort the data are extra layers that add delays.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    DoTheEvo (3rd August 2017)

Similar Threads

  1. Replies: 2
    Last Post: 1st May 2013, 06:49
  2. Replies: 0
    Last Post: 17th March 2010, 12:17
  3. SortFilterProxyModel data
    By gyre in forum Newbie
    Replies: 5
    Last Post: 6th December 2007, 22:09

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.