From 697aad4b8c0981eab097dc9d67d107c6e00a9ca3 Mon Sep 17 00:00:00 2001 From: Benjamin Buch Date: Tue, 4 Mar 2025 12:00:08 +0100 Subject: [PATCH] Add returnPressed signal to spin boxes This differs from `editingFinished`, which is also triggered when the focus is lost. `returnPressed` is useful if the user is to explicitly confirm the acceptance of the value, for example by means of an Apply button next to the SpinBox. The corresponding signal of the internal QLineEdit cannot be used as access to it is protected. Change-Id: Ie9a146212e6aeac5cc9e4592b1633156323426dd Fixes: QTBUG-99797 Reviewed-by: Christian Ehrlicher --- src/widgets/widgets/qabstractspinbox.cpp | 8 ++++++++ src/widgets/widgets/qabstractspinbox.h | 1 + .../widgets/qdoublespinbox/tst_qdoublespinbox.cpp | 11 +++++++++++ tests/auto/widgets/widgets/qspinbox/tst_qspinbox.cpp | 11 +++++++++++ 4 files changed, 31 insertions(+) diff --git a/src/widgets/widgets/qabstractspinbox.cpp b/src/widgets/widgets/qabstractspinbox.cpp index 75b32784760..e9cfb452e7d 100644 --- a/src/widgets/widgets/qabstractspinbox.cpp +++ b/src/widgets/widgets/qabstractspinbox.cpp @@ -112,6 +112,13 @@ using namespace std::chrono_literals; spinbox loses focus and when enter is pressed. */ +/*! + \fn void QAbstractSpinBox::returnPressed() + \since 6.10 + + This signal is emitted when the Return or Enter key is used. +*/ + /*! Constructs an abstract spinbox with the given \a parent with default \l wrapping, and \l alignment properties. @@ -1053,6 +1060,7 @@ void QAbstractSpinBox::keyPressEvent(QKeyEvent *event) selectAll(); event->ignore(); emit editingFinished(); + emit returnPressed(); emit d->edit->returnPressed(); return; diff --git a/src/widgets/widgets/qabstractspinbox.h b/src/widgets/widgets/qabstractspinbox.h index 91d3dcb8fcc..4d0a9319271 100644 --- a/src/widgets/widgets/qabstractspinbox.h +++ b/src/widgets/widgets/qabstractspinbox.h @@ -132,6 +132,7 @@ protected: virtual StepEnabled stepEnabled() const; Q_SIGNALS: void editingFinished(); + void returnPressed(); protected: QAbstractSpinBox(QAbstractSpinBoxPrivate &dd, QWidget *parent = nullptr); diff --git a/tests/auto/widgets/widgets/qdoublespinbox/tst_qdoublespinbox.cpp b/tests/auto/widgets/widgets/qdoublespinbox/tst_qdoublespinbox.cpp index 235ea524a7b..5766ffeb388 100644 --- a/tests/auto/widgets/widgets/qdoublespinbox/tst_qdoublespinbox.cpp +++ b/tests/auto/widgets/widgets/qdoublespinbox/tst_qdoublespinbox.cpp @@ -158,6 +158,8 @@ private slots: void editingFinished(); + void returnPressed(); + void removeAll(); void task199226_stateAfterEnter(); @@ -908,6 +910,15 @@ void tst_QDoubleSpinBox::editingFinished() QCOMPARE(editingFinishedSpy2.size(), 4); } +void tst_QDoubleSpinBox::returnPressed() +{ + QDoubleSpinBox spinBox; + QSignalSpy spyCurrentChanged(&spinBox, &QDoubleSpinBox::returnPressed); + spinBox.show(); + QTest::keyClick(&spinBox, Qt::Key_Return); + QCOMPARE(spyCurrentChanged.size(), 1); +} + void tst_QDoubleSpinBox::removeAll() { DoubleSpinBox spin(0); diff --git a/tests/auto/widgets/widgets/qspinbox/tst_qspinbox.cpp b/tests/auto/widgets/widgets/qspinbox/tst_qspinbox.cpp index 19570c04e0f..61579faa167 100644 --- a/tests/auto/widgets/widgets/qspinbox/tst_qspinbox.cpp +++ b/tests/auto/widgets/widgets/qspinbox/tst_qspinbox.cpp @@ -169,6 +169,8 @@ private slots: void editingFinished(); + void returnPressed(); + void valueFromTextAndValidate_data(); void valueFromTextAndValidate(); @@ -991,6 +993,15 @@ void tst_QSpinBox::editingFinished() QCOMPARE(editingFinishedSpy1.size(), 1); } +void tst_QSpinBox::returnPressed() +{ + QSpinBox spinBox; + QSignalSpy spyCurrentChanged(&spinBox, &QSpinBox::returnPressed); + spinBox.show(); + QTest::keyClick(&spinBox, Qt::Key_Return); + QCOMPARE(spyCurrentChanged.size(), 1); +} + void tst_QSpinBox::removeAll() { SpinBox spin(0);