Qt 5.3.0 & 5.3.1 Linux
我已经子类化了QSortFilterProxyModel并实现了lessThan()方法。当有人单击我连接到表格视图标题的时候,我会调用invalidate(),我看到它调用了'lessThan'但是表格视图从未更新。有人能告诉我我错过了什么吗?lessThan()绝对正确地排序了一些东西,因为我添加了一些打印语句来查看它被调用时内部发生了什么。我还尝试过添加table->repaint(),但这没有任何作用。
以下是我的代码:
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)));
然后是插槽:
void selectedColumnSlot(int col)
{
m_proxy_bookcase->sort(col, Qt::DescendingOrder);
m_proxy_bookcase->invalidate();
}