Results 1 to 6 of 6

Thread: About QAbstractProxyModel::sort(...) method

  1. #1
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default About QAbstractProxyModel::sort(...) method

    Hi,

    As you know there is method in both QAbstractProxyModel & QAbstractItemModel classes. What I want to know is - what is the first parameter of the sort method. For example;

    Qt Code:
    1. myFilterModel->sort(0, Qt::AscendingOrder);
    To copy to clipboard, switch view to plain text mode 

    0 refers to column here but according to what? I mean I have custom objects in my data model with various members/fields. How '0' in the sort method associated with my highlighted custom object's field?

  2. #2
    Join Date
    Oct 2011
    Posts
    27
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: About QAbstractProxyModel::sort(...) method

    Hi,

    not exactly : QAbstractItemModel has the sort method but QAbstractProxyModel has the sort method only by inheritance from QAbtractItemModel ...

    AND the docs says :

    void QAbstractItemModel::sort (int column , Qt::SortOrder order=Qt::AscendingOrder ) [virtual]
    Sorts the model by column in the given order.
    The base class implementation does nothing.
    So, it's up to you to do what you want with the "int column" parameter by reimplementing this method ...
    (It would be a good idea that the column parameter refers to the same entity than the one returned by "int QModelIndex::column() const" ...)

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

    Default Re: About QAbstractProxyModel::sort(...) method

    Your model decides which field it maps into which column.

    That's what models do: they provide an abstract interface to data. their main job is to define what a row means and what a column means in terms of the data they are working on.

    Cheers,
    _

  4. #4
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: About QAbstractProxyModel::sort(...) method

    Your model decides which field it maps into which column.

    That's what models do: they provide an abstract interface to data. their main job is to define what a row means and what a column means in terms of the data they are working on.

    Cheers,
    You're absolutely right but it would be much better if there is a sort method which takes a Role as a parameter.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,620
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: About QAbstractProxyModel::sort(...) method

    You mean like calling QSortFilterProxyModel::setSortRole() before sorting?

    You are, of course, quite welcome to subclass either the model or the proxy to reimplement sort() however you want. The function is virtual for a reason.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: About QAbstractProxyModel::sort(...) method

    Quote Originally Posted by zgulser View Post
    You're absolutely right but it would be much better if there is a sort method which takes a Role as a parameter.
    It would still need a column.

    The model can use whatever it wants for sorting, the sorting key doesn't even have to be accessible through a role.
    Of course the model can always allow to set a sorting role as additional information if a single column can be sorted differently depending on what role one is using.

    Cheers,
    _

Similar Threads

  1. Custom QAbstractProxyModel
    By alizadeh91 in forum Qt Programming
    Replies: 16
    Last Post: 5th January 2013, 23:52
  2. Calling a method from a non-member method
    By AndresBarbaRoja in forum Newbie
    Replies: 5
    Last Post: 19th March 2011, 10:38
  3. Replies: 1
    Last Post: 25th November 2010, 11:37
  4. How to sort a Qlist AND get a sort key?
    By agerlach in forum Qt Programming
    Replies: 3
    Last Post: 26th July 2010, 18:44
  5. Help with QAbstractProxyModel
    By killerwookie99 in forum Qt Programming
    Replies: 9
    Last Post: 12th September 2008, 22:13

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.