如何将 ItemDelegate 设置为仅应用于 QTreeView 中的父列

How to set ItemDelegate to only apply to parent column in QTreeView

本文关键字:QTreeView 应用于 ItemDelegate 设置      更新时间:2023-10-16

我将QCombobox插入QTreeView的第一列,如下所示。

view->setItemDelegateForColumn(0, new ComboBoxDelegate(view));

第 0 列中的节点有孩子,他们(如果我没记错的话(也是"0"列的一部分。因此,组合框也出现在那里。如何防止组合框出现在子分支中?

我现在拥有的:

>Combobox1
Combobox2

我希望它看起来如何:(其中"文本"取决于组合框的索引(

>Combobox1
Text

以下是创建组合框的一些函数:

ComboBoxDelegate::ComboBoxDelegate(QObject *parent): QItemDelegate(parent){
}

QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{
QComboBox *editor = new QComboBox(parent);
editor->addItem("Run");
editor->addItem("Run with SM");
editor->addItem("Kinetic Run");
}
return editor;
}

你应该做的是使用QModelIndex &index参数来获取行,然后说出类似的话:

if (!index.parent().isValid()) {
//draw combobox
}
else {
//don't draw
}