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 <ch.ehrlicher@gmx.de>
This commit is contained in:
Benjamin Buch 2025-03-04 12:00:08 +01:00
parent e323d46cda
commit 697aad4b8c
4 changed files with 31 additions and 0 deletions

View File

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

View File

@ -132,6 +132,7 @@ protected:
virtual StepEnabled stepEnabled() const;
Q_SIGNALS:
void editingFinished();
void returnPressed();
protected:
QAbstractSpinBox(QAbstractSpinBoxPrivate &dd, QWidget *parent = nullptr);

View File

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

View File

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