From 19b5163d0bef075d18f31959b27e67fbf265b7d7 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Thu, 8 Jul 2021 22:05:36 +0800 Subject: [PATCH] LineEdit: translate Ctrl+[ to Esc --- src/widgets/lineedit.cpp | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/widgets/lineedit.cpp b/src/widgets/lineedit.cpp index 0abc3bfe..e08a6f26 100644 --- a/src/widgets/lineedit.cpp +++ b/src/widgets/lineedit.cpp @@ -1,6 +1,7 @@ #include "lineedit.h" #include +#include #include @@ -21,19 +22,35 @@ void LineEdit::keyPressEvent(QKeyEvent *p_event) // Note that QKeyEvent starts with isAccepted() == true, so you do not // need to call QKeyEvent::accept() - just do not call the base class // implementation if you act upon the key. - bool accept = false; + bool accepted = false; int modifiers = p_event->modifiers(); switch (p_event->key()) { - case Qt::Key_H: - // Backspace. + case Qt::Key_BracketLeft: + { if (WidgetUtils::isViControlModifier(modifiers)) { - backspace(); - accept = true; + auto escEvent = new QKeyEvent(QEvent::KeyPress, + Qt::Key_Escape, + Qt::NoModifier); + QCoreApplication::postEvent(this, escEvent); + accepted = true; } break; + } + + case Qt::Key_H: + { + // Backspace. + if (WidgetUtils::isViControlModifier(modifiers)) { + backspace(); + accepted = true; + } + + break; + } case Qt::Key_W: + { // Delete one word backward. if (WidgetUtils::isViControlModifier(modifiers)) { if (!hasSelectedText()) { @@ -41,10 +58,11 @@ void LineEdit::keyPressEvent(QKeyEvent *p_event) } backspace(); - accept = true; + accepted = true; } break; + } case Qt::Key_U: { @@ -59,7 +77,7 @@ void LineEdit::keyPressEvent(QKeyEvent *p_event) } } - accept = true; + accepted = true; } break; @@ -69,7 +87,7 @@ void LineEdit::keyPressEvent(QKeyEvent *p_event) break; } - if (!accept) { + if (!accepted) { QLineEdit::keyPressEvent(p_event); } }