From 3310e13a17d2249a86fa533e350744c5593be54f Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Wed, 21 Oct 2020 17:39:42 +0200 Subject: Don't show QPushButton as hovered unless the mouse is within the bevel Previous fixes made QPushButton correctly respect the style sheet boxing model (as it's documented to do), ignoring clicks that were within the margin area of the button (ie outside the bevel). However, a hover state selector in the style sheet would still be used for the entire widget. Turn on mouse tracking for widgets that have a hover state selector, and handle mouseMoveEvent to set an explicit hovered state only when the mouse hits the button. Use that state to initialize the style option. Fixes: QTBUG-87706 Change-Id: I2f423b760c85cfab9faac4be44a5c7dcf2ba1c23 Reviewed-by: Richard Moe Gustavsen --- src/widgets/widgets/qpushbutton.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/widgets/widgets/qpushbutton.cpp') diff --git a/src/widgets/widgets/qpushbutton.cpp b/src/widgets/widgets/qpushbutton.cpp index 4faa8c7f67..3645b5e03d 100644 --- a/src/widgets/widgets/qpushbutton.cpp +++ b/src/widgets/widgets/qpushbutton.cpp @@ -330,6 +330,8 @@ void QPushButton::initStyleOption(QStyleOptionButton *option) const option->state |= QStyle::State_On; if (!d->flat && !d->down) option->state |= QStyle::State_Raised; + if (underMouse()) + option->state.setFlag(QStyle::State_MouseOver, d->hovering); option->text = d->text; option->icon = d->icon; option->iconSize = iconSize(); @@ -506,6 +508,25 @@ void QPushButton::focusOutEvent(QFocusEvent *e) #endif } +/*! + \reimp +*/ +void QPushButton::mouseMoveEvent(QMouseEvent *e) +{ + Q_D(QPushButton); + + if (testAttribute(Qt::WA_Hover)) { + bool hit = false; + if (underMouse()) + hit = hitButton(e->position().toPoint()); + + if (hit != d->hovering) { + update(rect()); + d->hovering = hit; + } + } +} + /*! \reimp */ -- cgit v1.2.3