简体   繁体   English

Qt ProxyModel性能问题

[英]Qt ProxyModel performance problems

So I have this Qt application with a QTreeView with a custom model (derived from QAbstractItemModel ) and a custom model proxy for filtering (derived from QSortFilterProxyModel ). 因此,我有一个QTreeView Qt应用程序, QTreeView带有一个自定义模型(来自QAbstractItemModel )和一个用于过滤的自定义模型代理(来自QSortFilterProxyModel )。 More or less straightforward (somewhat similar to the and works fine ) and works fine in terms of functionality. 或多或少的简单(有点类似于和可以正常工作 ),并且在功能上也可以正常工作。

The view shows a two-column tree with key-value pairs. 该视图显示具有键值对的两列树。 The keys are updated very rarely but the values are updated frequently (a lot of entries with several updates every second each). 键很少更新,但是值会频繁更新(很多条目每秒钟都有几次更新)。 In the proxy model I overload filterAcceptsRow to define visibility based on the key column. 在代理模型中,我重载了filterAcceptsRow以根据列定义可见性。 But every value change emits a dataChanged signal causing the view to call the proxy ( filterAcceptsRow ) again and that call is somewhat expensive (evaluate a regex on the element and its children). 但是每次更改都会发出dataChanged信号,从而导致视图再次调用代理( filterAcceptsRow ),并且该调用有些昂贵(对元素及其子元素评估正则表达式)。 There is some room for optimization in filterAcceptsRow by caching all the calculations but it would be preferred to limit the calls to that function. 通过缓存所有计算, filterAcceptsRow有一些优化的filterAcceptsRow ,但是最好限制对该函数的调用。 Can it somehow be limited what columns trigger the calls to the proxy? 可以限制某些列触发对代理的调用吗?

You have any advise for me? 您对我有什么建议吗?

Edit: Thanks for your input. 编辑:感谢您的输入。 I wasn't aware of dynamicSortFilter . 我不知道dynamicSortFilter I disabled dynamic sorting and also I connected the dataChanged -signal from the model to see if the key-column was changed and called invalidate in that case. 我禁用了动态排序,并且还从模型连接了dataChanged -signal,以查看键列是否已更改并在这种情况下称为无效。 That solved it. 那解决了。

If you look at the code for QSortFilterProxyModel you'll see that most of the work is only done when dynamicSortFilter is enabled. 如果查看QSortFilterProxyModel的代码,您会看到大多数工作仅在启用dynamicSortFilter时完成。 I guess that is the case for your code. 我想您的代码就是这种情况。

Can you live without it? 你能没有它吗? Maybe call invalidate() once every 100 updates or second, depending on which happens first. 也许每100次更新或每秒调用一次invalidate() ,这取决于首先发生的情况。

I had the same problem, solved it by emitting dataChanged signal from the proxy model itself, not from the main model. 我遇到了同样的问题,通过从代理模型本身而不是从主模型发出dataChanged信号解决了该问题。 Alternatively, you can disable dynamic sort/filtering ( dynamicSortFilter property) and call it manually when you want to filter or sort. 另外,您可以禁用动态排序/过滤( dynamicSortFilter属性),并在要过滤或排序时手动调用它。

Also, I'm not sure, but maybe simply specifying the column in dataChanged signal will do it for you. 另外,我不确定,但是也许只需在dataChanged信号中指定列即可为您完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM