简体   繁体   中英

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.

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". 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM