gpt4 book ai didi

c++ - 如果 QTreeView 项没有子项,则禁用 QAction

转载 作者:行者123 更新时间:2023-11-28 04:29:02 25 4
gpt4 key购买 nike

我从数据库中填充了 QTreeView

我将上下文菜单配置为:

ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);

我有寻找右键单击以打开项目上的上下文菜单的方法。

void MainWindow::on_treeView_customContextMenuRequested(const QPoint &pos)
{
QModelIndex idx = ui->treeView->indexAt(pos);
if (!idx.isValid())
{return;}
else{
QPoint globalPos = ui->treeView->mapToGlobal(pos);
QAction* selectedItem = contextMenu->exec(globalPos);
if (selectedItem){
qDebug () << selectedItem;
}
}

h.文件

QMenu *contextMenu;

如何检查从 QTreeView 中选择的项目是否不是任何项目的父项并且它有父项。

我是否应该在此处包含 QTreeViewQStandardItem 代码才能看到,或者这无关紧要?

最佳答案

Qt 文档。有专门的章节介绍这个主题:

Model/View Programming

我推荐它以获得概述。

关于OP的实际问题:

How do I check if the selected item from QTreeView is not a parent of any item & it has a parent.

QTreeView继承QAbstractItemView::model()它提供了指向 QAbstractItemModel 的指针这反过来又为呈现的 TreeView 项提供基础模型数据。

View 中提供的任何 QModelIndex 都应引用此模型。

QAbstractItemModel提供了多种方法来检索有关模型项的可视化和关系的数据。 QTreeView 使用它,但它也应该用于任何添加的功能。

因此,所选项目不是任何项目的父级 变成“所选项目没有子级”,QAbstractItemModel::hasChildren()适用于:

bool QAbstractItemModel::hasChildren(const QModelIndex &parent = QModelIndex()) const

Returns true if parent has any children; otherwise returns false.

Use rowCount() on the parent to find out the number of children.

Note that it is undefined behavior to report that a particular index hasChildren with this method if the same index has the flag Qt::ItemNeverHasChildren set.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

See also parent() and index().

并且它有一个父可以使用QAbstractItemModel::parent()检索:

QModelIndex QAbstractItemModel::parent(const QModelIndex &index) const

Returns the parent of the model item with the given index. If the item has no parent, an invalid QModelIndex is returned.

A common convention used in models that expose tree data structures is that only items in the first column have children. For that case, when reimplementing this function in a subclass the column of the returned QModelIndex would be 0.

When reimplementing this function in a subclass, be careful to avoid calling QModelIndex member functions, such as QModelIndex::parent(), since indexes belonging to your model will simply call your implementation, leading to infinite recursion.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

See also createIndex().

综合起来,OPs 函数应该是这样的:

void MainWindow::on_treeView_customContextMenuRequested(const QPoint &pos)
{
QModelIndex idx = ui->treeView->indexAt(pos);
if (!idx.isValid()
|| !ui->treeView->model()->hasChildren(idx)
&& !ui->treeView->model()->parent(idx).isValid()) {
return;
// bail out -> no context menu for leaf nodes or toplevel nodes
} else {
QPoint globalPos = ui->treeView->mapToGlobal(pos);
QAction* selectedItem = contextMenu->exec(globalPos);
if (selectedItem) {
qDebug () << selectedItem;
}
}
}

我不太确定这是否完全符合 OP 要求的行为。可能有必要修复该条件,但这应该没有那么难。

关于c++ - 如果 QTreeView 项没有子项,则禁用 QAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53467239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com