4

Flat QPushButtons have no indication of mouse hovering. Is there a way to set the styling for the flat button hover state so that it matches the style of the normal button hover state (or something similar)?

QPushButton types

I don't want to use style sheets if it means deviating away from the default look or having to design the button styles from scratch.

101
  • 8,514
  • 6
  • 43
  • 69

2 Answers2

7

One way of doing it is deriving QPushButton:

pushbutton.h:

#ifndef PUSHBUTTON_H
#define PUSHBUTTON_H

#include <QPushButton>

class PushButton : public QPushButton
{
    Q_OBJECT
public:
    explicit PushButton(QWidget *parent = 0);

protected:
    bool event(QEvent * event);

};

#endif // PUSHBUTTON_H

pushbutton.cpp:

#include "pushbutton.h"

#include <QEvent>

PushButton::PushButton(QWidget *parent) :
    QPushButton(parent)
{
    setFlat(true);
}

bool PushButton::event(QEvent *event)
{
    if(event->type() == QEvent::HoverEnter)
    {
        setFlat(false);
    }

    if(event->type() == QEvent::HoverLeave)
    {
        setFlat(true);
    }

    return QPushButton::event(event);
}
Jacob Krieg
  • 2,834
  • 15
  • 68
  • 140
1

"Qt way" is to implement your own QProxyStyle or even QStyle. It's much more efficient in compare with style sheets. Inside Qt, stylesheets are automatically converted to corresponding QProxyStyle objects.

But implementing your own QStyle is harder than using QSS.

101
  • 8,514
  • 6
  • 43
  • 69
Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61