diff --git a/src/widgets/widgets/qabstractbutton.cpp b/src/widgets/widgets/qabstractbutton.cpp index 965278265a..e413b3b87a 100644 --- a/src/widgets/widgets/qabstractbutton.cpp +++ b/src/widgets/widgets/qabstractbutton.cpp @@ -1293,8 +1293,10 @@ void QAbstractButton::focusInEvent(QFocusEvent *e) void QAbstractButton::focusOutEvent(QFocusEvent *e) { Q_D(QAbstractButton); - if (e->reason() != Qt::PopupFocusReason) + if (e->reason() != Qt::PopupFocusReason && d->down) { d->down = false; + d->emitReleased(); + } QWidget::focusOutEvent(e); } @@ -1304,8 +1306,10 @@ void QAbstractButton::changeEvent(QEvent *e) Q_D(QAbstractButton); switch (e->type()) { case QEvent::EnabledChange: - if (!isEnabled()) - setDown(false); + if (!isEnabled() && d->down) { + d->down = false; + d->emitReleased(); + } break; default: d->sizeHint = QSize(); diff --git a/tests/auto/widgets/widgets/qpushbutton/tst_qpushbutton.cpp b/tests/auto/widgets/widgets/qpushbutton/tst_qpushbutton.cpp index c53a7cc5ca..4fd8b99acf 100644 --- a/tests/auto/widgets/widgets/qpushbutton/tst_qpushbutton.cpp +++ b/tests/auto/widgets/widgets/qpushbutton/tst_qpushbutton.cpp @@ -76,6 +76,7 @@ private slots: void sizeHint_data(); void sizeHint(); void taskQTBUG_20191_shortcutWithKeypadModifer(); + void emitReleasedAfterChange(); protected slots: void resetCounters(); @@ -663,5 +664,36 @@ void tst_QPushButton::taskQTBUG_20191_shortcutWithKeypadModifer() QCOMPARE(spy2.count(), 1); } +void tst_QPushButton::emitReleasedAfterChange() +{ + QPushButton *button1 = new QPushButton("A"); + QPushButton *button2 = new QPushButton("B"); + QVBoxLayout *layout = new QVBoxLayout(); + layout->addWidget(button1); + layout->addWidget(button2); + QDialog dialog; + dialog.setLayout(layout); + dialog.show(); + QTest::qWaitForWindowExposed(&dialog); + QApplication::setActiveWindow(&dialog); + button1->setFocus(); + + QSignalSpy spy(button1, SIGNAL(released())); + QTest::mousePress(button1, Qt::LeftButton); + QVERIFY(button1->isDown()); + QTest::keyClick(&dialog, Qt::Key_Tab); + QVERIFY(!button1->isDown()); + QCOMPARE(spy.count(), 1); + spy.clear(); + + QCOMPARE(spy.count(), 0); + button1->setFocus(); + QTest::mousePress(button1, Qt::LeftButton); + QVERIFY(button1->isDown()); + button1->setEnabled(false); + QVERIFY(!button1->isDown()); + QCOMPARE(spy.count(), 1); +} + QTEST_MAIN(tst_QPushButton) #include "tst_qpushbutton.moc"