From d9be9fedfb15c3eca69adedf9e2e95c0df9643e7 Mon Sep 17 00:00:00 2001 From: Laszlo Papp Date: Sat, 7 May 2022 09:55:31 +0100 Subject: Add a clear action to QKeySequenceEdit This can be really helpful for deleting shortcuts that one may never intend to use so that they do not accidentally get in the way while interacting with the user interface. The workaround is to try to find the QLineEdit child of the QKeySequenceEdit widget, but it is suboptimal. Some first-class citizen API for this would be a more stable and cleaner solution as opposed to poking implementation details as a user of the QKeySequenceEdit API. The clear action would be configurable and opt-in as not everyone may potentially want this to be turned on. Or at least, not by default anyway. Also, not to suddenly change existing QKeySequenceEdit uses with surprising UI changes after a Qt upgrade, this needs to be opt-in. Moreover, some customers may have already used findChild() to add actions to QKeySequenceEdit in this way on the client side. So, if it was enabled by default, they would suddenly get multiple actions potentially for the same clear action. This would be certainly undesirable. The new clear property is based on the corresponding QLineEdit API for consistency. [ChangeLog][QtWidgets][QKeySequenceEdit] Added a clear action to QKeySequenceEdit to be able to remove existing hotkey configurations. Change-Id: I3a90bfacd49bff10c1284abb155d7edd4f537900 Reviewed-by: Richard Moe Gustavsen --- src/widgets/widgets/qkeysequenceedit.cpp | 34 ++++++++++++++++++++++++++++++-- src/widgets/widgets/qkeysequenceedit.h | 4 ++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/widgets/widgets/qkeysequenceedit.cpp b/src/widgets/widgets/qkeysequenceedit.cpp index 97ee915037..674044e030 100644 --- a/src/widgets/widgets/qkeysequenceedit.cpp +++ b/src/widgets/widgets/qkeysequenceedit.cpp @@ -55,6 +55,13 @@ void QKeySequenceEditPrivate::init() lineEdit = new QLineEdit(q); lineEdit->setObjectName(QStringLiteral("qt_keysequenceedit_lineedit")); + lineEdit->setClearButtonEnabled(false); + q->connect(lineEdit, &QLineEdit::textChanged, [q](const QString& text) { + // Clear the shortcut if the user clicked on the clear icon + if (text.isEmpty()) + q->clear(); + }); + keyNum = 0; prevKey = -1; releaseTimer = 0; @@ -73,8 +80,6 @@ void QKeySequenceEditPrivate::init() q->setFocusPolicy(Qt::StrongFocus); q->setAttribute(Qt::WA_MacShowFocusRect, true); q->setAttribute(Qt::WA_InputMethodEnabled, false); - - // TODO: add clear button } int QKeySequenceEditPrivate::translateModifiers(Qt::KeyboardModifiers state, const QString &text) @@ -175,6 +180,31 @@ QKeySequence QKeySequenceEdit::keySequence() const return d->keySequence; } +/*! + \property QKeySequenceEdit::clearButtonEnabled + \brief Whether the key sequence edit displays a clear button when it is not + empty. + + If enabled, the key sequence edit displays a trailing \e clear button when + it contains some text, otherwise the line edit does not show a clear button + (the default). + + \since 6.4 +*/ +void QKeySequenceEdit::setClearButtonEnabled(bool enable) +{ + Q_D(QKeySequenceEdit); + + d->lineEdit->setClearButtonEnabled(enable); +} + +bool QKeySequenceEdit::isClearButtonEnabled() const +{ + Q_D(const QKeySequenceEdit); + + return d->lineEdit->isClearButtonEnabled(); +} + void QKeySequenceEdit::setKeySequence(const QKeySequence &keySequence) { Q_D(QKeySequenceEdit); diff --git a/src/widgets/widgets/qkeysequenceedit.h b/src/widgets/widgets/qkeysequenceedit.h index 999b400963..f0dea69ae3 100644 --- a/src/widgets/widgets/qkeysequenceedit.h +++ b/src/widgets/widgets/qkeysequenceedit.h @@ -54,6 +54,7 @@ class Q_WIDGETS_EXPORT QKeySequenceEdit : public QWidget Q_OBJECT Q_PROPERTY(QKeySequence keySequence READ keySequence WRITE setKeySequence NOTIFY keySequenceChanged USER true) + Q_PROPERTY(bool clearButtonEnabled READ isClearButtonEnabled WRITE setClearButtonEnabled) public: explicit QKeySequenceEdit(QWidget *parent = nullptr); @@ -62,6 +63,9 @@ public: QKeySequence keySequence() const; + void setClearButtonEnabled(bool enable); + bool isClearButtonEnabled() const; + public Q_SLOTS: void setKeySequence(const QKeySequence &keySequence); void clear(); -- cgit v1.2.3