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/qmdisubwindow/tst_qmdisubwindow.cpp | 28 +++++++++++++++++----- 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'tests/auto/widgets/widgets/qmdisubwindow/tst_qmdisubwindow.cpp') diff --git a/tests/auto/widgets/widgets/qmdisubwindow/tst_qmdisubwindow.cpp b/tests/auto/widgets/widgets/qmdisubwindow/tst_qmdisubwindow.cpp index 2c5cb88ed6..1c19bf96d6 100644 --- a/tests/auto/widgets/widgets/qmdisubwindow/tst_qmdisubwindow.cpp +++ b/tests/auto/widgets/widgets/qmdisubwindow/tst_qmdisubwindow.cpp @@ -152,6 +152,22 @@ Q_DECLARE_METATYPE(Qt::WindowType); Q_DECLARE_METATYPE(Qt::WindowFlags); Q_DECLARE_METATYPE(QMdiSubWindow*); +class TestPushButton : public QPushButton +{ +public: + TestPushButton(const QString &title, QWidget *parent = nullptr) + : QPushButton(title, parent) + { + } + +protected: + // don't rely on style-specific button behavior in test + bool hitButton(const QPoint &point) const override + { + return rect().contains(point); + } +}; + class tst_QMdiSubWindow : public QObject { Q_OBJECT @@ -390,7 +406,7 @@ void tst_QMdiSubWindow::mainWindowSupport() // the maximized subwindow's title is imposed onto the main window's titlebar. if (!nativeMenuBar) { QCOMPARE(mainWindow.windowTitle(), QString()); - QMdiSubWindow *window = workspace->addSubWindow(new QPushButton(QLatin1String("Test"))); + QMdiSubWindow *window = workspace->addSubWindow(new TestPushButton(QLatin1String("Test"))); QString expectedTitle = QLatin1String("MainWindow's title is empty"); window->setWindowTitle(expectedTitle); QCOMPARE(window->windowTitle(), expectedTitle); @@ -1298,7 +1314,7 @@ void tst_QMdiSubWindow::changeFocusWithTab() QTest::keyPress(widget, Qt::Key_Backtab); QCOMPARE(QApplication::focusWidget(), static_cast(firstLineEdit)); - QMdiSubWindow *window = mdiArea.addSubWindow(new QPushButton); + QMdiSubWindow *window = mdiArea.addSubWindow(new TestPushButton(QLatin1String("test"))); window->show(); QCOMPARE(mdiArea.activeSubWindow(), window); @@ -1907,7 +1923,7 @@ void tst_QMdiSubWindow::setFont() QSKIP("This test function is unstable in CI, please see QTBUG-22544"); QMdiArea mdiArea; mdiArea.setWindowTitle(QLatin1String(QTest::currentTestFunction())); - QMdiSubWindow *subWindow = mdiArea.addSubWindow(new QPushButton(QLatin1String("test"))); + QMdiSubWindow *subWindow = mdiArea.addSubWindow(new TestPushButton(QLatin1String("test"))); subWindow->resize(300, 100); subWindow->setWindowTitle(QLatin1String("Window title")); mdiArea.show(); @@ -2057,19 +2073,19 @@ void tst_QMdiSubWindow::task_233197() QMenuBar *menuBar = mainWindow->menuBar(); // force creation of a menubar Q_UNUSED(menuBar); - QPushButton *focus1 = new QPushButton(QLatin1String("Focus 1"), mainWindow); + QPushButton *focus1 = new TestPushButton(QLatin1String("Focus 1"), mainWindow); QObject::connect(focus1, &QAbstractButton::clicked, subWindow1, QOverload<>::of(&QWidget::setFocus)); focus1->move(5, 30); focus1->show(); - QPushButton *focus2 = new QPushButton(QLatin1String("Focus 2"), mainWindow); + QPushButton *focus2 = new TestPushButton(QLatin1String("Focus 2"), mainWindow); QObject::connect(focus2, &QAbstractButton::clicked, subWindow2, QOverload<>::of(&QWidget::setFocus)); focus2->move(5, 60); focus2->show(); - QPushButton *close = new QPushButton(QLatin1String("Close"), mainWindow); + QPushButton *close = new TestPushButton(QLatin1String("Close"), mainWindow); QObject::connect(close, &QAbstractButton::clicked, mainWindow, &QWidget::close); close->move(5, 90); close->show(); -- cgit v1.2.3