Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 691
101 Avatar asked Mar 19 '23 07:03

101


1 Answers

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);
}
like image 69
Jacob Krieg Avatar answered Apr 08 '23 16:04

Jacob Krieg