QAbstractTableModel empty last row, without sorting

I have my own table model (DragDropTableModel) subclassed from QAbstractTableModel. Inside it I would like for the last row to remain always empty.

I implement it by adding insertRows() into setData() function:

bool DragDropTableModel :: setData (const QModelIndex &index, const QVariant &value, int role) { ... if (role == Qt::EditRole) { ... if (row == this->rowCount() - 1) insertRows(this->rowCount(), 1); } }

After that I have QSortFilterProxyModel installed on it. The problem is: when I do sorting, the last row is also sorted. But I want to make it not obeying sorting and to stand on the same last position. So no matter the sorting order is (Ascending or Descending) the last row should always be empty.

How to make it? May be I should use another approach to realize "making empty row" in my model?

-------------Problems Reply------------

Thanks, murison. Your idea worked. I set up a value "EMPTY_ROW" for Qt::UserRole for the items of empty row. After that I reimplemented lessThan() method in QSortFilterProxyModel:

bool MySortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
if ( == "EMPTY_ROW")
return (this->sortOrder() == Qt::DescendingOrder);
else if ( == "EMPTY_ROW")
return (this->sortOrder() == Qt::AscendingOrder);
return QSortFilterProxyModel::lessThan(left, right);

So this row always appears in the bottom of the table after sorting. Also it's nescesary not to forget to clear Qt::UserRole when this empty row is filled with data.

Category:c# Views:4 Time:2017-09-10
Tags: sorting

Related post

Copyright (C), All Rights Reserved.

processed in 0.146 (s). 10 q(s)