Вы можете использовать Анимация .
MyButton.h
#include <QPushButton>
#include <QColor>
#include <QPropertyAnimation>
class MyButton : public QPushButton
{
Q_OBJECT
Q_PROPERTY(QColor color READ GetColor WRITE SetColor)
public:
explicit MyButton(QWidget *parent = 0);
void SetColor(const QColor& color);
const QColor& GetColor() const;
protected:
bool eventFilter(QObject *obj, QEvent *e);
private:
QColor m_currentColor;
QPropertyAnimation m_colorAnimation;
void StartHoverEnterAnimation();
void StartHoverLeaveAnimation();
};
MyButton.cpp
#include "MyButton.h"
#include <QEvent>
#include <QDebug>
MyButton::MyButton(QWidget *parent) :
QPushButton(parent),
m_colorAnimation(this, "color")
{
this->installEventFilter(this);
}
void MyButton::SetColor(const QColor& color)
{
m_currentColor = color;
QString css = "QPushButton { border-radius: 5px; ";
css.append("border: 1.5px solid rgb(91,231,255); ");
QString strColor = QString("rgb(%1, %2, %3)").arg(color.red()).arg(color.green()).arg(color.blue());
css.append("background-color: " + strColor + "; }");
setStyleSheet(css);
}
const QColor& MyButton::GetColor() const
{
return m_currentColor;
}
bool MyButton::eventFilter(QObject *obj, QEvent *e)
{
if (e->type() == QEvent::HoverEnter) {
StartHoverEnterAnimation();
}
if (e->type() == QEvent::HoverLeave) {
StartHoverLeaveAnimation();
}
return false;
}
void MyButton::StartHoverEnterAnimation()
{
m_colorAnimation.stop();
m_colorAnimation.setDuration(900); //set your transition
m_colorAnimation.setStartValue(GetColor()); //starts from current color
m_colorAnimation.setEndValue(QColor(100, 100, 100));//set your hover color
m_colorAnimation.setEasingCurve(QEasingCurve::Linear);//animation style
m_colorAnimation.start();
}
void MyButton::StartHoverLeaveAnimation()
{
m_colorAnimation.stop();
m_colorAnimation.setDuration(900); //set your transition
m_colorAnimation.setStartValue(GetColor()); //starts from current color
m_colorAnimation.setEndValue(QColor(255, 0, 0));//set your regular color
m_colorAnimation.setEasingCurve(QEasingCurve::Linear);//animation style
m_colorAnimation.start();
}
Это будет конфликтовать с настройкой внешнего qss. Так что установите все QSS в SetColor
.