【发布时间】:2016-10-14 18:23:17
【问题描述】:
之前,我向a question 询问了多列过滤,我们需要表示适合多个过滤模式的行。
现在在处理大表(big 我的意思是大约 200,000 行和 4 列)时,如果我们有一个那么大的表(通常这对于过滤模式的前 2 个字符来说是最差的),过滤会变慢。
那么您对此有何建议?
注意:我有自己的高性能源数据模型(而不是 QStandardItemModel),基于 this 示例,女巫在大约 1 秒内为我提供该行数的视图
编辑 1
从此改变我的方法:
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
if (/* filtering is enable*/) {
bool _res = sourceModel()->data(sourceModel()->index(source_row, 0, source_parent)).toString().contains( /*RegExp for column 0*/);
for (int col = 0; col < columnCount(); col++) {
_res &= sourceModel()->data(sourceModel()->index(source_row, col + 1, source_parent)).toString().contains(/*RegExp for column col + 1*/);
}
return _res;
}
return true;
}
对此:
bool DataFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
if (_enable) {
return (sourceModel()->index(source_row, 0, source_parent.child(source_row, 0)).data().toString().contains( /*string for column 0*/ ))
&& sourceModel()->index(source_row, 1, source_parent.child(source_row, 1)).data().toString().contains(/*string for column 1*/))
&& sourceModel()->index(source_row, 2, source_parent.child(source_row, 2)).data().toString().contains(/*string for column 2*/))
&& sourceModel()->index(source_row, 3, source_parent.child(source_row, 3)).data().toString().contains(/*string for column 3*/));
}
return true;
}
看起来很完美。现在过滤效果就像一个魅力,没有延迟
【问题讨论】:
-
1.描述您的模型性能。 2.不要使用正则表达式进行过滤,编写自己的方法。 3. 如果你真的需要大数据支持 - 将内存中的 SQLite 作为模型和过滤的来源。
-
您可以放弃 QSortFilterProxyModel 并用您自己的专用实现替换它;因为您知道您的应用的特殊需求,您可能能够将您的实现设计为比 QSortFilterProxyModel 更高效,后者必须是通用的,并且不能对其交互的代码的行为做出任何假设。
-
@JeremyFriesner。所以你说我们应该忘记
QSortFilterProxyModel。如果我们只需要过滤,在源数据模型(源自QAbstractTableModel)中实现过滤是个好主意吗?如果没有,我会听你的建议。 -
@DmitrySazonov。如果我是对的,您确认:使用字符串代替 RegExp 并在自己的方法(设置器)中设置过滤器字符串?实际上我从 MSSQL 获取数据到我的模型,正如我所说,它对于 200,000 行非常快,当我们使用过滤时,数据(该视图使用它)在内存中
-
当使用
&&时,尝试帮助它快速失败——把最不可能的情况放在第一位。这将减少必须评估连词的后续分支的次数。
标签: c++ qt qt5 qabstractitemmodel qsortfilterproxymodel