我正在尝试对 QPushButton 单击事件应用柔和的颜色更改。我使用 QPropertyAnimation 的第一种方法就像一个魅力。
标题:
class myAnim : public QWidget
{
Q_OBJECT
Q_PROPERTY(QColor color READ color WRITE setColor)
public:
explicit myAnim(QWidget *parent = 0);
void setColor (QColor color){
setStyleSheet(QString(" QPushButton { background-color: rgb(%1, %2, %3); }").arg(color.red()).arg(color.green()).arg(color.blue()));
}
和来源:
QPropertyAnimation *anim = new QPropertyAnimation(this, "color");
anim->setDuration(300); // duration in ms
anim->setStartValue(QColor(0, 0, 0);
anim->setEndValue(QColor(249, 249, 249));
anim->start();
但是由于我的 Button 确实有一个线性渐变作为背景,所以我需要更改不止一种颜色。尝试更改标题,如下所示:
void setColor (QColor color[3]){
setStyleSheet(QString("QPushButton { background: qlineargradient(x1:0, y1:0, x2:0, y2:1,") +
QString("stop: 0 rgba(%1, %2, %3, 255),").arg( color[0].red() ).arg( color[0].green() ).arg( color[0].blue() ) +
QString("stop: 0.5 rgba(%1, %2, %3, 255),").arg( color[1].red() ).arg( color[1].green() ).arg( color[1].blue() ) +
QString("stop: 0.6 rgba(%1, %2, %3, 255),").arg( color[2].red() ).arg( color[2].green() ).arg( color[2].blue() ) +
QString("stop: 1 rgba(%1, %2, %3, 255),").arg( color[0].red() ).arg( color[0].green() ).arg( color[0].blue() ));
}
我的问题:如何正确编辑源文件中的“setStartValue”和“setEndValue”?
编辑 1: 我的应用程序中的按钮如下所示: button_1
QPushButton 的样式表:
background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #f9f9f9 , stop: 0.5 #B5B5B5 , stop: 0.6 #D6D6D6 , stop:1 #f9f9f9 );
按下事件的样式表:
background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #005da8, stop: 0.5 #2882cc, stop: 0.6 #418ecd, stop:1 #005da8);
点击银色渐变后变成蓝色渐变。释放后,它们应该再次柔和地淡入银色外观。如前所述,第一种方法正是这样做的,但只有一种纯色。我以前从未使用过 QPainter 或自定义paintEvent,因此我们将不胜感激!
谢谢!
米查