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:
bool MaterialDBSortModel
::filterAcceptsColumn(int source_column,
const QModelIndex & source_parent
) const { if (source_column == 2)
return false;
return true;
}
bool MaterialDBSortModel::filterAcceptsColumn(int source_column, const QModelIndex & source_parent ) const {
if (source_column == 2)
return false;
return true;
}
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:
QVariant leftData
= sourceModel
()->data
(left
);
QVariant rightData
= sourceModel
()->data
(right
);
qDebug() << left.column(); // prints 2 when I clicked on the third column
// but since I filtered the third column (index 2), I expected a print out of 3 !?!
// ... snip ... do comparison
// ... snip
}
bool MaterialDBSortModel::lessThan(const QModelIndex & left, const QModelIndex & right) const {
QVariant leftData = sourceModel()->data(left);
QVariant rightData = sourceModel()->data(right);
qDebug() << left.column(); // prints 2 when I clicked on the third column
// but since I filtered the third column (index 2), I expected a print out of 3 !?!
// ... snip ... do comparison
// ... snip
}
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
Bookmarks