101
101

Reputation: 8999

Change styling of flat QPushButton hover state (without using style sheets)

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.

Upvotes: 4

Views: 3610

Answers (2)

Dmitry Sazonov
Dmitry Sazonov

Reputation: 8994

"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.

Upvotes: 1

Jacob Krieg
Jacob Krieg

Reputation: 3174

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);
}

Upvotes: 7

Related Questions