summaryrefslogtreecommitdiffstats
path: root/src/widgets
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2020-10-21 17:39:42 +0200
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2020-10-22 02:43:40 +0200
commit3310e13a17d2249a86fa533e350744c5593be54f (patch)
tree610feb59ccde7fd855ae6e35a9f07c8a67c4f9e3 /src/widgets
parent875a7fad52e88a86eb963b534e9a7c3e27d7e7ce (diff)
Don't show QPushButton as hovered unless the mouse is within the bevelHEADdev
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 <richard.gustavsen@qt.io>
Diffstat (limited to 'src/widgets')
-rw-r--r--src/widgets/styles/qstylesheetstyle.cpp1
-rw-r--r--src/widgets/widgets/qpushbutton.cpp21
-rw-r--r--src/widgets/widgets/qpushbutton.h1
-rw-r--r--src/widgets/widgets/qpushbutton_p.h5
4 files changed, 27 insertions, 1 deletions
diff --git a/src/widgets/styles/qstylesheetstyle.cpp b/src/widgets/styles/qstylesheetstyle.cpp
index 472d3c05db..3a27d5b240 100644
--- a/src/widgets/styles/qstylesheetstyle.cpp
+++ b/src/widgets/styles/qstylesheetstyle.cpp
@@ -2863,6 +2863,7 @@ void QStyleSheetStyle::polish(QWidget *w)
if ( cssClass & PseudoClass_Hover || negated & PseudoClass_Hover) {
w->setAttribute(Qt::WA_Hover);
embeddedWidget(w)->setAttribute(Qt::WA_Hover);
+ embeddedWidget(w)->setMouseTracking(true);
}
}
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();
@@ -509,6 +511,25 @@ void QPushButton::focusOutEvent(QFocusEvent *e)
/*!
\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
+*/
bool QPushButton::hitButton(const QPoint &pos) const
{
QStyleOptionButton option;
diff --git a/src/widgets/widgets/qpushbutton.h b/src/widgets/widgets/qpushbutton.h
index 5452f4227b..eeaa50bb4c 100644
--- a/src/widgets/widgets/qpushbutton.h
+++ b/src/widgets/widgets/qpushbutton.h
@@ -93,6 +93,7 @@ protected:
void keyPressEvent(QKeyEvent *) override;
void focusInEvent(QFocusEvent *) override;
void focusOutEvent(QFocusEvent *) override;
+ void mouseMoveEvent(QMouseEvent *) override;
virtual void initStyleOption(QStyleOptionButton *option) const;
bool hitButton(const QPoint &pos) const override;
QPushButton(QPushButtonPrivate &dd, QWidget* parent = nullptr);
diff --git a/src/widgets/widgets/qpushbutton_p.h b/src/widgets/widgets/qpushbutton_p.h
index a180b27e33..e28a6d71df 100644
--- a/src/widgets/widgets/qpushbutton_p.h
+++ b/src/widgets/widgets/qpushbutton_p.h
@@ -69,7 +69,9 @@ public:
QPushButtonPrivate()
: QAbstractButtonPrivate(QSizePolicy::PushButton), autoDefault(Auto),
- defaultButton(false), flat(false), menuOpen(false), lastAutoDefault(false) {}
+ defaultButton(false), flat(false), menuOpen(false), hovering(false),
+ lastAutoDefault(false)
+ {}
inline void init() { resetLayoutItemMargins(); }
static QPushButtonPrivate* get(QPushButton *b) { return b->d_func(); }
@@ -89,6 +91,7 @@ public:
uint defaultButton : 1;
uint flat : 1;
uint menuOpen : 1;
+ uint hovering : 1;
mutable uint lastAutoDefault : 1;
};