diff --git a/include/attributeTableDelegate.h b/include/attributeTableDelegate.h index 6d0ca4c..03ef27a 100644 --- a/include/attributeTableDelegate.h +++ b/include/attributeTableDelegate.h @@ -31,7 +31,7 @@ signals: protected: bool eventFilter(QObject*, QEvent*) override; - //void closeEvent(QCloseEvent*) override; + //void mousePressEvent(QMouseEvent*) override; private slots: void onTextChanged(); diff --git a/source/attributeTableDelegate.cpp b/source/attributeTableDelegate.cpp index 22b0b10..af20a5e 100644 --- a/source/attributeTableDelegate.cpp +++ b/source/attributeTableDelegate.cpp @@ -24,6 +24,7 @@ TextEditWidget::TextEditWidget(QWidget* parent) connect(ui->btnConfirm, &QPushButton::clicked, this, &TextEditWidget::onBtnClicked_confirm); connect(ui->btnCancle, &QPushButton::clicked, this, &TextEditWidget::onBtnClicked_cancle); + installEventFilter(this); ui->plainTextEdit->installEventFilter(this); } @@ -34,7 +35,9 @@ TextEditWidget::~TextEditWidget() bool TextEditWidget::eventFilter(QObject* obj, QEvent* event) { - if (/*obj == this && */event->type() == QEvent::KeyPress) + if(obj == this && event->type() == QEvent::MouseButtonPress) + return true; //戒断鼠标事件,防止传递给父组件(tableView)触发单元格切换造成的editor关闭 + else if (obj == ui->plainTextEdit && event->type() == QEvent::KeyPress) { QKeyEvent* pKeyEvent = static_cast(event); if (pKeyEvent->key() == Qt::Key_Escape) @@ -46,6 +49,11 @@ bool TextEditWidget::eventFilter(QObject* obj, QEvent* event) return QWidget::eventFilter(obj, event); } +// void TextEditWidget::mousePressEvent(QMouseEvent* event) +// { +// event->accept(); +// } + QString TextEditWidget::editText() { return ui->plainTextEdit->toPlainText(); @@ -233,6 +241,10 @@ QWidget* AttributeTableDelegate::createEditor(QWidget *parent, const QStyleOptio if(m_tableView) m_tableView->setFocus(); }); + connect(textEditor, &TextEditWidget::closeWidget, this, [=]{ + if(m_tableView) + m_tableView->setFocus(); + }); //延迟获取焦点,防止可能因渲染未完成失效 QTimer::singleShot(10, this, [textEditor]() { textEditor->setFocusToTextEdit();