问题描述
我实现了一个自定义的QAbstractTableModle,我使用std :: vector为我的数据对象。
I implemented a custom QAbstractTableModle and I'm using a std::vector for my data objects.
现在我想实现sort得到我的表按列排序。
这基本上是我做的:
Now I wanted to implement the sort() method, to get my table sorted by column.That's basically what I do:
void SBStateTableModel::sort (int column, Qt::SortOrder order)
{
emit layoutAboutToBeChanged();
switch (column)
{
case Address:
if (order == Qt::DescendingOrder)
std::sort(states.begin(), states.end(), addr_comp_desc);
else
std::sort(states.begin(), states.end(), addr_comp_asc);
default:
return;
}
emit layoutChanged();
}
但是只发布layoutChanged()不会重绘视图。当标记一行并通过它们循环时,它们在被突出显示时被更新。
But emitting layoutChanged() alone doesn't redraw the view. When mark a row and cycle through them, they get updated as they are being highlighted.
本文档还提到更新持久索引。这里有些人建议,这实际上不是必要的。
我甚至不知道如何去做。获取列表与persistentIndexList(),然后我必须排序。但std :: sort不是一个稳定的排序。我不知道如何匹配持久索引与我的向量索引。
The documentation also speaks about updating the persistent indexes. Some people on here have suggested, that this is in fact not necessary.I'm not even sure how to go about it. Getting the list with persistentIndexList() and then I have to sort it. But std::sort is not a stable sort. I'm not sure how to match the persistent indices with my vector indices.
编辑:
'case' !因此,函数将在发出layoutChanged信号之前返回。
There was just a "break" missing in the 'case'! So the function would return before emitting the layoutChanged signal.
推荐答案
D'oh!
我已经准备好了解Qt源代码。但是当我单步走过我的代码,我看到光标跳转到默认情况下的return语句。
I was ready to dig into the Qt Source Code. But as I single stepped through my code, I saw the cursor jumping to the return statement in the 'default' case.
我刚刚忘记添加一个'到我的交换机情况!这只是一个简单的错误:((
)现在它与layoutChanged完全一样。
I had just forgotten to add a 'break' to my switch-case! It was just a simple fall-through error :((It works now perfectly fine with "layoutChanged".
这篇关于排序Qt表模型 - QTableView不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!