我有一棵树,其父节点为 A 、 B 、 C 。每个节点都有子节点。我想允许仅对一个父节点下的子节点进行多重选择。任何指针,我如何使用 QTreeview 做到这一点?
A-> D,E,F
B-> G, H, I
C-> J, K, L
因此,应允许对 D、E、F 或 G、H、I 进行多重选择,而不允许对 D、G、H 等进行多重选择。
谢谢
最佳答案
这是一种效果相当好的方法。为您的 View 分配模型后, Hook 到选择模型的更改参数。
connect(treeView->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(processSelection(QItemSelection, QItemSelection)));
然后编写一个函数,每次更改时都会更改选择,以便满足您的要求。
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);
}
当在大树的大面积上拖动范围时,我注意到一些非常小的减速,但除此之外,它似乎工作得很好。
关于qt - QTreeview 中唯一父节点的子节点的多重选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22200351/