Changing the style of the flat hover state of a QPushButton (without using style sheets)

The flat ones QPushButtons

do not have any indication of the mouse hovering. Is there a way to set the style for the flat button hover state to match the style of the normal button hover state (or something similar)?

QPushButton types

I don't want to use stylesheets if that means deviating from the look and feel or having to create button styles from scratch.

+3


source to share


2 answers


One way to do this is to output 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);
}

      

+5


source


"Qt way" - implement your own QProxyStyle

or even QStyle

. This is much more efficient than style sheets. Within Qt, stylesheets are automatically converted to the appropriate objects QProxyStyle

.



But implementing your own is QStyle

more difficult and then using QSS.

+1


source







All Articles