QTreeWidget支持双击编辑Item节点的内容

QTreeWidget使用过程中经常会需要对Item进行编辑,目前比较方便的是双击Item,然后Item呈现编辑的状态,完成编辑后更新Item的内容。要实现这样的功能,其实只要将Item设置为可编辑即可,但是QTreeWiget默认的可编辑状态不好看,因此我们需要对可编辑的状态重新定义,这里仍然用到代理绘制。首先看效果图:

原始加载的图如下:
在这里插入图片描述
双击编辑的图:
在这里插入图片描述
编辑完成后的图:
在这里插入图片描述
下面详细介绍实现:
1 添加Item时设置为可编辑

zhangsanroot->setFlags(zhangsanroot->flags() | Qt::ItemIsEditable);

2 为树设置代理绘制

ui->treeWidget->setItemDelegate(new TreeDelegate(0));

3 代理绘制具体实现
一个自定义的delegate也可以直接提供一个编辑器,而不是使用内置的编辑器工厂(editor item factory)。如果你需要这种功能,那么需要实现一下几个函数:

createEditor(): 返回修改数据的组件;
setEditorData(): 为editor提供编辑的原始数据;
setModelData(): 根据editor 的数据更新model的数据。

头文件实现如下:

#include
class TreeDelegate :  public QStyledItemDelegate
{
       Q_OBJECT
public:
       TreeDelegate( int durationColumn, QObject *parent = 0);
       void paint(QPainter *painter,  const QStyleOptionViewItem &option,  const QModelIndex &index)  const;
       QWidget *createEditor(QWidget *parent,  const QStyleOptionViewItem &option,  const QModelIndex &index)  const;
       void setEditorData(QWidget *editor,  const QModelIndex &index)  const;
       void setModelData(QWidget *editor, QAbstractItemModel *model,  const QModelIndex &index)  const;
private slots:
        void commitAndCloseEditor();
private:
        int durationColumn;
};

CPP代码实现如下:

TreeDelegate::TreeDelegate(int durationColumn, QObject *parent)
        : QStyledItemDelegate(parent)
{
         this->durationColumn = durationColumn;
}

/*此处paint函数可不重写*/
void TreeDelegate::paint(QPainter *painter,  const QStyleOptionViewItem &option,  const QModelIndex &index)  const
{
     QStyledItemDelegate::paint(painter, option, index);
}

/*编辑器是一个LineEidt*/
QWidget *TreeDelegate::createEditor(QWidget *parent,  const QStyleOptionViewItem &option,  const QModelIndex &index)  const
{
    if (index.column() == durationColumn) {
        QLineEdit *nameEdit =  new QLineEdit(parent);
        connect(nameEdit, SIGNAL(editingFinished()),  this, SLOT(commitAndCloseEditor()));
        return nameEdit;
    } else {
        return QStyledItemDelegate::createEditor(parent, option, index);
    }
}

void TreeDelegate::commitAndCloseEditor()
{
    QLineEdit *editor = qobject_cast<QLineEdit *>(sender());
    if(editor->text() == ""){
        return;
    }
    emit commitData(editor);
    emit closeEditor(editor);
}

/*默认为空*/
void TreeDelegate::setEditorData(QWidget *editor,  const QModelIndex &index)  const
{
    if (index.column() == durationColumn) {
            QLineEdit *nameEdit = qobject_cast<QLineEdit *>(editor);
            nameEdit->setText("");
    }  else {
            QStyledItemDelegate::setEditorData(editor, index);
    }
}

/*编辑器有输入值时则更新数据*/
void TreeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,  const QModelIndex &index)  const
{
    if (index.column() == durationColumn) {
        QLineEdit *nameEdit = qobject_cast<QLineEdit *>(editor);
        if(nameEdit->text() == ""){
            return;
        }
        model->setData(index, nameEdit->text());
    }  else {
        QStyledItemDelegate::setModelData(editor, model, index);
    }
}

你可能感兴趣的:(QTreeWidget支持双击编辑Item节点的内容)