我有一个TableView
,显示QSortFilterProxyModel
中包含的信息(其模型是QAbstractTableModel
)
有没有办法修复/冻结特定属性设置为 true 的所有行TableView's
顶部,同时保持其他行的过滤和排序?
为了将特定行始终放在表视图的顶部,我在QSortFilterProxyModel
的lessThan
方法中添加了以下逻辑:
// Get left and right row
int leftRow = left.row();
int rightRow = right.row();
// Get specific data from left row
QModelIndex leftIndex = sourceModel()->index(leftRow, SPECIFIC_DATA_INDEX);
QString leftProperty = sourceModel()->data(leftIndex).toString();
// Get specific data from right row
QModelIndex rightIndex = sourceModel()->index(rightRow, SPECIFIC_DATA_INDEX);
QString rightProperty = sourceModel()->data(rightIndex).toString();
if(leftProperty .compare("Invalid") == 0) // put left on top if it has a specific property
if(sortOrder() == Qt::AscendingOrder) {
return true;
} else {
return false;
}
} else if(rightProperty .compare("Invalid") == 0) { // put right on top if it has a specific property
if(sortOrder() == Qt::AscendingOrder) {
return false;
} else {
return true;
}
}