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<QLineEdit *>()
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 <richard.gustavsen@qt.io>
This commit is contained in:
Laszlo Papp 2022-05-07 09:55:31 +01:00 committed by Laszlo Papp
parent 4872392925
commit d9be9fedfb
2 changed files with 36 additions and 2 deletions

View File

@ -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);

View File

@ -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();