user1703942
user1703942

Reputation: 327

Multiple selection for the child nodes of one only parentin QTreeview?

I have a Tree with parent nodes as A , B , C . Each node has child nodes. I want to allow the multiple selections for only child nodes under one parent node. Any pointer, that how can i do that using QTreeview ?

A-> D,E,F   
B-> G, H, I   
C-> J, K, L

So multiple selection should be allowed for D,E,F or G,H,I and not for D, G, H for example.

Thank you

Upvotes: 2

Views: 2125

Answers (1)

Portponky
Portponky

Reputation: 61

Here is one method which works fairly well. After your view is assigned a model, hook in to the selectionModel's changed parameter.

connect(treeView->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(processSelection(QItemSelection, QItemSelection)));

Then write a function which alters selection every time it changes, so that it fits your requirement.

void MyClass::processSelection(const QItemSelection& selected, const QItemSelection& deselected)
{
    if (selected.empty())
        return;

    QItemSelectionModel* selectionModel = treeView->selectionModel();

    QItemSelection selection = selectionModel->selection();
    const QModelIndex parent = treeView->currentIndex().parent();

    QItemSelection invalid;

    Q_FOREACH(QModelIndex index, selection.indexes())
    {
        if (index.parent() == parent)
            continue;

        invalid.select(index, index);
    }

    selectionModel->select(invalid, QItemSelectionModel::Deselect);
}

I've noticed some very minor slowdown when dragging ranges across large areas of a large tree, but other than that it seems to work well.

Upvotes: 4

Related Questions