gpt4 book ai didi

qt - QSortFilterProxyModel QTableView 5.3 不排序/更新?

转载 作者:行者123 更新时间:2023-12-02 10:39:11 29 4
gpt4 key购买 nike

Qt 5.3.0 和 5.3.1 Linux

我已经对 QSortFilterProxyModel 进行了子类化并实现了 lessThan()。当有人单击表格 View 标题(我已“连接”到)时我调用 invalidate(),我看到它调用“lessThan”,但 tableview 从来没有更新。有人能告诉我我缺少什么吗? 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();
}

最佳答案

考虑这一行:

table->setModel(bookcaseModel);

您的表格似乎显示了底层模型,而不是代理。应该是:

table->setModel(m_proxy_bookcase);

当您使用代理对模型进行排序时,它不会修改源模型;只有代理知道调用 sort() 后元素的顺序。这就是为什么您的 View 必须显示代理而不是源模型。

并且(我可能是错的)我认为在 sort() 之后调用 invalidate() 没有用。

关于qt - QSortFilterProxyModel QTableView 5.3 不排序/更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25000608/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com