diff --git a/src/widgets/widgets/qpushbutton.cpp b/src/widgets/widgets/qpushbutton.cpp index 74ec79fae2c..7ce445303dc 100644 --- a/src/widgets/widgets/qpushbutton.cpp +++ b/src/widgets/widgets/qpushbutton.cpp @@ -581,7 +581,7 @@ void QPushButtonPrivate::_q_popupPressed() //menu visibility to avoid flicker on button release menuOpen = true; QObject::connect(menu, &QMenu::aboutToHide, - q, [q]{ q->setDown(false); }, Qt::SingleShotConnection); + q, [q, this]{ menuOpen = false; q->setDown(false); }, Qt::SingleShotConnection); menu->popup(menuPos); } diff --git a/tests/auto/widgets/widgets/qpushbutton/tst_qpushbutton.cpp b/tests/auto/widgets/widgets/qpushbutton/tst_qpushbutton.cpp index 8acbfeb6cff..760fa7ff3f5 100644 --- a/tests/auto/widgets/widgets/qpushbutton/tst_qpushbutton.cpp +++ b/tests/auto/widgets/widgets/qpushbutton/tst_qpushbutton.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -54,6 +55,7 @@ private slots: void hitButton(); void iconOnlyStyleSheet(); void mousePressAndMove(); + void reactToMenuClosed(); protected slots: void resetCounters(); @@ -760,5 +762,51 @@ void tst_QPushButton::mousePressAndMove() QCOMPARE(releaseSpy.size(), 1); } +/* + Test checking that a QPushButton with a QMenu has a sunken style only + when the menu is open + QTBUG-120976 +*/ +void tst_QPushButton::reactToMenuClosed() +{ + // create a subclass of QPushButton to expose the initStyleOption method + class PushButton : public QPushButton { + public: + virtual void initStyleOption(QStyleOptionButton *option) const override + { + QPushButton::initStyleOption(option); + } + }; + + PushButton button; + QStyleOptionButton opt; + QMenu menu; + + // add a menu to the button + menu.addAction(tr("string")); + button.setMenu(&menu); + + // give the button a size and show it + button.setGeometry(0, 0, 50, 50); + button.show(); + QVERIFY(QTest::qWaitForWindowExposed(&button)); + + // click the button to open the menu + QTest::mouseClick(&button, Qt::LeftButton); + + // check the menu is visible and the button style is sunken + QTRY_VERIFY(menu.isVisible()); + button.initStyleOption(&opt); + QVERIFY(opt.state.testFlag(QStyle::StateFlag::State_Sunken)); + + // close the menu + menu.close(); + + // check the menu isn't visible and the style isn't sunken + QTRY_VERIFY(!menu.isVisible()); + button.initStyleOption(&opt); + QVERIFY(!opt.state.testFlag(QStyle::StateFlag::State_Sunken)); +} + QTEST_MAIN(tst_QPushButton) #include "tst_qpushbutton.moc"