QT 双击编辑QTreeWidget中Item的内容

Qt中QTreeWidget是常用的一种控件,为了实现更好的用户体验,一般会双击编辑Item中的内容,点击其他地方编辑完成。

class ElemTree : public QTreeWidget {
    Q_OBJECT

public:
    BattleElemTree(QWidget *parent);
    ~BattleElemTree();
private slots:
    void slotDoubleClickItem(QTreeWidgetItem *item, int col);
    void slotFinishEdit();//双击编辑完成
};
#include "ElemTree.h"
#include 
#include 

ElemTree::ElemTree(QWidget *parent)
: QTreeWidget(parent) {
    geoItem = nullptr;
    connect(this, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(slotDoubleClickItem(QTreeWidgetItem *, int)));
}

ElemTree::~ElemTree() {

}

void ElemTree::slotDoubleClickItem(QTreeWidgetItem *item, int col) {
    doubleClickItem = nullptr;
    if (col == 0) {
        doubleClickItem = item;
        QLineEdit* lineEdit = new QLineEdit(this);
        lineEdit->setText(QStringLiteral("新建要素"));
        connect(lineEdit, SIGNAL(editingFinished()), this, SLOT(slotFinishEdit()));
        this->setItemWidget(doubleClickItem, 0, lineEdit);
    }
}

//槽函数 - 编辑完成
void ElemTree::slotFinishEdit() {
    if (doubleClickItem != nullptr) {
        QLineEdit *edit = qobject_cast(this->itemWidget(doubleClickItem, 0));
        if (!edit) {
            return;
        }
        QString text = edit->text();
        this->removeItemWidget(doubleClickItem, 0);
        doubleClickItem->setText(0, text);
    }
}

QT 双击编辑QTreeWidget中Item的内容_第1张图片

你可能感兴趣的:(qt)