From 6e1d70ae12baae4610356ec7b69635ad75a97b4e Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Wed, 22 Jan 2020 13:51:01 +0100 Subject: QPushButton: only trigger button when click occurs within the bevel rect On the mac, the push button's bevel doesn't cover the entire widget rectangle, but is smaller to leave space for focus frame, shadow, and in general to meet style guidelines. Without this change, a click anywhere inside the widget would activate the button. QAbstractButton::hitButton can be reimplemented to limit the area in which the button is triggered. However, getting the rectangle also requires an addition to QStyle, so that we can query QStyle::subElementRect for the actual area the button's bevel covers. As a side effect, tests that use QPushButton and assume that it responds to clicks at position 0,0 have to be fixed so that they don't fail on mac. Change-Id: I01b60a763bccf39090aee5b2369af300f922d226 Fixes: QTBUG-81452 Reviewed-by: Richard Moe Gustavsen --- .../widgets/qbuttongroup/tst_qbuttongroup.cpp | 37 ++++++++++++++++------ 1 file changed, 27 insertions(+), 10 deletions(-) (limited to 'tests/auto/widgets/widgets/qbuttongroup/tst_qbuttongroup.cpp') diff --git a/tests/auto/widgets/widgets/qbuttongroup/tst_qbuttongroup.cpp b/tests/auto/widgets/widgets/qbuttongroup/tst_qbuttongroup.cpp index 47dfc86a69..279fe49e3a 100644 --- a/tests/auto/widgets/widgets/qbuttongroup/tst_qbuttongroup.cpp +++ b/tests/auto/widgets/widgets/qbuttongroup/tst_qbuttongroup.cpp @@ -58,6 +58,23 @@ protected: } }; +class TestPushButton : public QPushButton +{ +public: + TestPushButton(QWidget *parent = nullptr) + : QPushButton(parent) + {} + TestPushButton(const QString &title, QWidget *parent = nullptr) + : QPushButton(title, parent) + {} + +protected: + bool hitButton(const QPoint &pos) const override + { + return rect().contains(pos); + } +}; + #include class tst_QButtonGroup : public QObject @@ -97,7 +114,7 @@ void tst_QButtonGroup::arrowKeyNavigation() QGroupBox g1("1", &dlg); QHBoxLayout g1layout(&g1); QRadioButton bt1("Radio1", &g1); - QPushButton pb("PB", &g1); + TestPushButton pb("PB", &g1); QLineEdit le(&g1); QRadioButton bt2("Radio2", &g1); g1layout.addWidget(&bt1); @@ -231,9 +248,9 @@ void tst_QButtonGroup::exclusive() { QDialog dlg(0); QHBoxLayout layout(&dlg); - QPushButton *pushButton1 = new QPushButton(&dlg); - QPushButton *pushButton2 = new QPushButton(&dlg); - QPushButton *pushButton3 = new QPushButton(&dlg); + TestPushButton *pushButton1 = new TestPushButton(&dlg); + TestPushButton *pushButton2 = new TestPushButton(&dlg); + TestPushButton *pushButton3 = new TestPushButton(&dlg); pushButton1->setCheckable(true); pushButton2->setCheckable(true); pushButton3->setCheckable(true); @@ -271,9 +288,9 @@ void tst_QButtonGroup::exclusive() void tst_QButtonGroup::testSignals() { QButtonGroup buttons; - QPushButton pb1; - QPushButton pb2; - QPushButton pb3; + TestPushButton pb1; + TestPushButton pb2; + TestPushButton pb3; buttons.addButton(&pb1); buttons.addButton(&pb2, 23); buttons.addButton(&pb3); @@ -390,9 +407,9 @@ void tst_QButtonGroup::checkedButton() { QButtonGroup buttons; buttons.setExclusive(false); - QPushButton pb1; + TestPushButton pb1; pb1.setCheckable(true); - QPushButton pb2; + TestPushButton pb2; pb2.setCheckable(true); buttons.addButton(&pb1); buttons.addButton(&pb2, 23); @@ -456,7 +473,7 @@ void tst_QButtonGroup::task209485_removeFromGroupInEventHandler() QFETCH(int, signalCount); qRegisterMetaType("QAbstractButton *"); - QPushButton *button = new QPushButton; + TestPushButton *button = new TestPushButton; QButtonGroup group; group.addButton(button); -- cgit v1.2.3