3
votes

Qt 5.3.0 & 5.3.1 Linux

I've subclassed QSortFilterProxyModel and implemented lessThan(). When someone clicks the tableview header (which Ive 'connect'ed to) and I call invalidate(), I see it call 'lessThan' but the tableview never updates. Can someone tell me what Im missing? lessThan() is definitely sorting things properly as I put some print statements to see what was happening inside there when it was called. I've also tried adding table->repaint() which doesn't do anything.

Here's my code:

QTableView *table = m_ui->tableView;
table->resize(930, 200);
table->setAlternatingRowColors(true);
table->setSelectionMode(QAbstractItemView::SingleSelection);
table->setSelectionBehavior(QAbstractItemView::SelectRows);
table->verticalHeader()->hide();

QStringList header;
header << "ID";
header << "Prefix";
header << "First";
header << "M";
header << "Last";
int cols = header.size();

BookcaseModel *bookcaseModel = new BookcaseModel(this, cols, header);
m_proxy_bookcase = new SortFilterProxyModelBookcase(this);
m_proxy_bookcase->setSourceModel(bookcaseModel);
m_proxy_bookcase->sort(0, Qt::AscendingOrder);
m_proxy_bookcase->setDynamicSortFilter(true);
m_proxy_bookcase->setSortRole(Qt::DisplayRole);

table->setModel(bookcaseModel);
table->setSortingEnabled(true);
table->horizontalHeader()->setSortIndicator(0, Qt::AscendingOrder);
table->horizontalHeader()->setSectionsClickable(true);

connect(table->horizontalHeader(), SIGNAL(sectionClicked(int)),
        this, SLOT(selectedColumnSlot(int)));

Then the slot:

void selectedColumnSlot(int col) 
{
    m_proxy_bookcase->sort(col, Qt::DescendingOrder);
    m_proxy_bookcase->invalidate();
}
1

1 Answers

4
votes

Considering this line:

table->setModel(bookcaseModel);

It seems your table is showing the underlying model, not the proxy. It should be:

table->setModel(m_proxy_bookcase);

When you sort a model using a proxy, it doesn't modify the source model; only the proxy knows the order of elements after the call to sort(). That's why your view must show the proxy rather than the source model.

And (I might be wrong) I don't think the call to invalidate() after sort() is useful.