Making only one column of a QTreeWidgetItem editable

后端 未结 10 1145
无人共我
无人共我 2020-12-06 04:18

I have a QTreeWidgetItem with two columns of data, is there any way to make only the second column editable? When I do the following:

QTreeWidge         


        
10条回答
  •  醉酒成梦
    2020-12-06 04:45

    class EditorDelegate : public QItemDelegate
    {
        Q_OBJECT
    
    public:
        EditorDelegate(QObject *parent):QItemDelegate(parent){};
        QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    };
    
    QWidget* EditorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        if(index.column() == 1)
        {
            return QItemDelegate::createEditor(parent, option, index);
        }
        return nullptr;
    }
    

    In the QTreeWidget:

    myQTreeWidget::myQTreeWidget()
    {
        EditorDelegate *d = new EditorDelegate(this);
        this->setItemDelegate(d);
    }
    

提交回复
热议问题