qt - QTreeview 中唯一父节点的子节点的多重选择?

标签 qt qtreeview multipleselection

我有一棵树,其父节点为 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/

相关文章:

ios - 当我在 UITableview 上滚动时,多个选定行的复选标记消失了。如何解决这个问题?

ios - 选择和取消选择UITableView行

c++ - 如何在不使用 QMainWindow 的情况下向 QWidget 添加背景图像?

python - 如何抑制 Qt GUI 应用程序?

string - 用斜线分割字符串

c++ - QAbstractItemModel data() 永远不会被调用

ios - didDeselectRowAtIndexPath 未被调用

c++ - 哪种机制使 Oracle session 在服务器上保持事件状态?

c++ - QTreeView如何获取当前选中项的绝对路径

c++ - QTreeView在主要功能之外不起作用