tags:

views:

61

answers:

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)

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?

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;

Thanks in advance.

+1  A: 

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.

andref
Thanks! Exactly what I needed.
Jonathan Thomas