Fastest way to select lots of items in QTreeView programatically?

  • Hey

    I have few thousand of items to select in my treeView and something tells me that this code:

        for (int x = 0; x < nodeList.size(); ++x) {
            selectionModel()->select(nodeList[x]->index(), QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);

    will not be very fast. How can I do this action quicker? Should I create a QItemSelection there per each item and then bundle them all up under 1 larger selection range(via QItemSelection.merge?) and select that? The items may/may not be next to each other.

    Something like :

        QItemSelection sel;
        for (int x = 0; x < nodeList.size(); ++x) {
            QModelIndex inx = nodeList[x]->index();
            sel.merge(QItemSelection(inx, inx), QItemSelectionModel::Select | QItemSelectionModel::Rows);
        selectionModel()->select(sel, QItemSelectionModel::Select | QItemSelectionModel::Rows);


  • Lifetime Qt Champion


    Are these items all random ?

  • @SGaist Yup

    I could maybe group all ModelIndexes in 1 vector, then sort them, then try and produce a "groups of indexes" that are next to each other so that I make lesss QItemSelections/merge actions?

  • Moderators

    I found out the hard way that for large selections using the QItemSelectionModel::Rows option is a massive performance killer. The algorithm for extending the selection doesn't seem to be very good.
    I had a case where manually creating QItemSelection with items for all columns and then selecting it in one call without that flag gave me even 30x boost. Give it a try.

Log in to reply