[英]How to set ItemDelegate to only apply to parent column in QTreeView
I am inserting QComboboxes into the first column of a QTreeView as follows. 我将QComboboxes插入QTreeView的第一列,如下所示。
view->setItemDelegateForColumn(0, new ComboBoxDelegate(view));
The nodes in the 0th column have children, who (if i'm not mistaken) are also part of column "0". 第0列中的节点有孩子,这些孩子(如果我没记错的话)也是“ 0”列的一部分。 Therefore the comboboxes also appear there.
因此,组合框也出现在此处。 How can I prevent the Comboboxes from appearing in the child branch?
如何防止组合框出现在子分支中?
What I have now: 我现在所拥有的:
>Combobox1
Combobox2
How I want it to look: (Where "text" depends on index of combobox) 我希望它的外观:(其中“文本”取决于组合框的索引)
>Combobox1
Text
Here are some of the functions that create the combobox: 以下是一些创建组合框的函数:
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;
}
What you should do is user the QModelIndex &index
parameter to get the row, and then say something like: 您应该使用
QModelIndex &index
参数来获取行,然后输入以下内容:
if (!index.parent().isValid()) {
//draw combobox
}
else {
//don't draw
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.