Results 1 to 3 of 3

Thread: QSortFilterProxyModel - lessThan sorts wrong column

  1. #1
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    108
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Post QSortFilterProxyModel - lessThan sorts wrong column

    Hi there,

    this post is about Qt 4.3.3.

    I inherited a QSortFilterProxyModel and reimplemeted both filterAcceptsColumn() and lessThan(). setSortingEnabled(true) is set for my table view.

    Things work fine to the point when I remove a column in filterAcceptsColumn(). Say, I have 5 columns in my model and in filterAcceptsColumn() I write:

    Qt Code:
    1. bool MaterialDBSortModel::filterAcceptsColumn(int source_column, const QModelIndex & source_parent ) const {
    2. if (source_column == 2)
    3. return false;
    4. return true;
    5. }
    To copy to clipboard, switch view to plain text mode 

    to remove the third column. Now my table shows only 4 columns and here is the problem. When I click on column number 3 (corresponding to column 4 in my source model), it sorts the table based on the content of column #3 in the source model (my lessThan() function gets the data for the source model).

    That is, if I use the following code:

    Qt Code:
    1. bool MaterialDBSortModel::lessThan(const QModelIndex & left, const QModelIndex & right) const {
    2. QVariant leftData = sourceModel()->data(left);
    3. QVariant rightData = sourceModel()->data(right);
    4.  
    5. qDebug() << left.column(); // prints 2 when I clicked on the third column
    6. // but since I filtered the third column (index 2), I expected a print out of 3 !?!
    7.  
    8. // ... snip ... do comparison
    9. // ... snip
    10. }
    To copy to clipboard, switch view to plain text mode 

    just as in the example. So, I'm wondering whether lessThan() gets source ModelIndices or proxy ModelIndices. Since when I click on column 3 the model index left has the column 3, so my guess is that I get proxyIndex. But using QSortFilterProxy::data() or mapToSource() with the either parameter 'left' or 'right' always results in a crash.

    What is the correct way to get the correct column data or the correct source model indices?

    Thanks,
    Andreas
    Andreas

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel - lessThan sorts wrong column

    Sounds like a bug in Qt to me. Did you report it?

  3. #3
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    108
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Exclamation Re: QSortFilterProxyModel - lessThan sorts wrong column

    It's a bug. Reported for 4.4.0 rc1 (and previous versions of Qt), here's the link to the tracker:

    TaskTracker entry 219948

    Workaround: Manually remember the number of filtered columns.

    Most efficient way to do this:

    Whenever a filter property of your QSortFilterProxyModel model changes, update a vector of type:
    Qt Code:
    1. filteredColumnCount[proxy_column]
    To copy to clipboard, switch view to plain text mode 

    Example: you have 5 columns, column with index 2 is filtered out, the vector has the content:

    Qt Code:
    1. filteredColumnCount[0] = 0
    2. filteredColumnCount[1] = 0
    3. filteredColumnCount[2] = 1
    4. filteredColumnCount[3] = 1
    To copy to clipboard, switch view to plain text mode 

    and the sort code needs to manually shift the sort column:

    Qt Code:
    1. bool DemoSortFilterProxyModel::lessThan(const QModelIndex & left, const QModelIndex & right) const {
    2. // get the column to filter
    3. int col = left.column();
    4. // shift column to match source model column
    5. col += filteredColumnCount[col];
    6. return mydata(left.row(), col) < mydata(right.row(), col);
    7. }
    To copy to clipboard, switch view to plain text mode 
    Andreas

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.