如何线程排序QSortFilterProxyModel? [英] How to thread the sorting of a QSortFilterProxyModel?

查看:462
本文介绍了如何线程排序QSortFilterProxyModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的主视图包含一个级别(无子级)QTreeView,显示平均30,000个项目。由于项目的创建方式,它们会被插入到未排序的模型中。这意味着在应用程序启动时,我必须按照字母顺序对视图中的项进行排序,这需要接近1秒,留下无响应的灰色屏幕,直到完成。 (因为窗口还没有绘制)

The main view of my application contains a one-level (no children) QTreeView that displays on average 30,000 items. Due to the way the items are created, they are inserted into the model unsorted. This means that on application startup I have to sort the items in the view alphabetically, which takes nearly 1 second, leaving an unresponsive grey screen until it is done. (Since the window hasn't painted yet)

有没有办法我可以得到一个QSortFilerProxyModel排序到一个单独的线程,或有任何其他替代方法

Is there any way I could get the sorting of a QSortFilerProxyModel into a separate thread, or are there any other alternative ways to approach this problem?

这是我的lessThan()代码,供参考:(左和右是传递给函数的两个QModelIndexes)

Here's my lessThan() code, for reference: (left and right are the two QModelIndexes passed to the function)

    QString leftString = left.data(PackageModel::NameRole).toString();
    QString rightString = right.data(PackageModel::NameRole).toString();

    return leftString < rightString;

提前感谢。

推荐答案

不要对视图中的项目进行排序。将它们添加到临时列表,然后使用 QtConcurrent: :run 。完成后(使用 QFutureWatcher 知道什么时候),设置你的模型。正在执行排序时,您可以显示请稍候消息或响声。

Don't sort the items in the view. Add them to a temporary list and sort that list using QtConcurrent::run. When done (use a QFutureWatcher to know when), set up your model. While sorting is being performed, you can display a "please wait" message or a throbber.

这篇关于如何线程排序QSortFilterProxyModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆