summaryrefslogtreecommitdiffstats
path: root/src/widgets/widgets/qkeysequenceedit.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/widgets/widgets/qkeysequenceedit.cpp')
-rw-r--r--src/widgets/widgets/qkeysequenceedit.cpp69
1 files changed, 54 insertions, 15 deletions
diff --git a/src/widgets/widgets/qkeysequenceedit.cpp b/src/widgets/widgets/qkeysequenceedit.cpp
index c7d82019f1..65ed7a465a 100644
--- a/src/widgets/widgets/qkeysequenceedit.cpp
+++ b/src/widgets/widgets/qkeysequenceedit.cpp
@@ -20,7 +20,7 @@ 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) {
+ q->connect(lineEdit, &QLineEdit::textChanged, q, [q](const QString& text) {
// Clear the shortcut if the user clicked on the clear icon
if (text.isEmpty())
q->clear();
@@ -29,6 +29,7 @@ void QKeySequenceEditPrivate::init()
keyNum = 0;
prevKey = -1;
releaseTimer = 0;
+ finishingKeyCombinations = {Qt::Key_Tab, Qt::Key_Backtab};
QVBoxLayout *layout = new QVBoxLayout(q);
layout->setContentsMargins(0, 0, 0, 0);
@@ -176,12 +177,8 @@ bool QKeySequenceEdit::isClearButtonEnabled() const
\property QKeySequenceEdit::maximumSequenceLength
\brief The maximum sequence length.
- The value is clamped to [1-4] inclusive, i.e. the maximum value of the
- maximum sequence length is 4 driven by QKeySequence. The minimum value is
- 1, which can be useful for single sequence, like a typical shortcut.
-
- The QKeySequence stored in QKeySequenceEdit is truncated if longer than the
- value of this property.
+ The maximum number of key sequences a user can enter. The value needs to
+ be between 1 and 4, with 4 being the default.
\since 6.5
*/
@@ -209,6 +206,31 @@ void QKeySequenceEdit::setMaximumSequenceLength(qsizetype count)
}
}
+/*!
+ \property QKeySequenceEdit::finishingKeyCombinations
+ \brief The list of key combinations that finish editing the key sequences.
+
+ Any combination in the list will finish the editing of key sequences.
+ All other key combinations can be recorded as part of a key sequence. By
+ default, Qt::Key_Tab and Qt::Key_Backtab will finish recording the key
+ sequence.
+
+ \since 6.5
+*/
+void QKeySequenceEdit::setFinishingKeyCombinations(const QList<QKeyCombination> &finishingKeyCombinations)
+{
+ Q_D(QKeySequenceEdit);
+
+ d->finishingKeyCombinations = finishingKeyCombinations;
+}
+
+QList<QKeyCombination> QKeySequenceEdit::finishingKeyCombinations() const
+{
+ Q_D(const QKeySequenceEdit);
+
+ return d->finishingKeyCombinations;
+}
+
void QKeySequenceEdit::setKeySequence(const QKeySequence &keySequence)
{
Q_D(QKeySequenceEdit);
@@ -260,13 +282,23 @@ void QKeySequenceEdit::clear()
*/
bool QKeySequenceEdit::event(QEvent *e)
{
+ Q_D(const QKeySequenceEdit);
+
switch (e->type()) {
case QEvent::Shortcut:
return true;
case QEvent::ShortcutOverride:
e->accept();
return true;
- default :
+ case QEvent::KeyPress: {
+ QKeyEvent *ke = static_cast<QKeyEvent *>(e);
+ if (!d->finishingKeyCombinations.contains(ke->keyCombination())) {
+ keyPressEvent(ke);
+ return true;
+ }
+ }
+ break;
+ default:
break;
}
@@ -280,6 +312,11 @@ void QKeySequenceEdit::keyPressEvent(QKeyEvent *e)
{
Q_D(QKeySequenceEdit);
+ if (d->finishingKeyCombinations.contains(e->keyCombination())) {
+ d->finishEditing();
+ return;
+ }
+
int nextKey = e->key();
if (d->prevKey == -1) {
@@ -307,22 +344,23 @@ void QKeySequenceEdit::keyPressEvent(QKeyEvent *e)
return;
if (e->modifiers() & Qt::ShiftModifier) {
- QList<int> possibleKeys = QKeyMapper::possibleKeys(e);
- int pkTotal = possibleKeys.count();
+ const QList<QKeyCombination> possibleKeys = QKeyMapper::possibleKeys(e);
+ int pkTotal = possibleKeys.size();
if (!pkTotal)
return;
bool found = false;
for (int i = 0; i < possibleKeys.size(); ++i) {
- if (possibleKeys.at(i) - nextKey == int(e->modifiers())
- || (possibleKeys.at(i) == nextKey && e->modifiers() == Qt::ShiftModifier)) {
- nextKey = possibleKeys.at(i);
+ const int key = possibleKeys.at(i).toCombined();
+ if (key - nextKey == int(e->modifiers())
+ || (key == nextKey && e->modifiers() == Qt::ShiftModifier)) {
+ nextKey = key;
found = true;
break;
}
}
// Use as fallback
if (!found)
- nextKey = possibleKeys.first();
+ nextKey = possibleKeys.first().toCombined();
} else {
nextKey |= d->translateModifiers(e->modifiers(), e->text());
}
@@ -377,7 +415,8 @@ void QKeySequenceEdit::timerEvent(QTimerEvent *e)
void QKeySequenceEdit::focusOutEvent(QFocusEvent *e)
{
Q_D(QKeySequenceEdit);
- d->finishEditing();
+ if (e->reason() != Qt::PopupFocusReason)
+ d->finishEditing();
QWidget::focusOutEvent(e);
}