【发布时间】:2016-06-25 11:57:52
【问题描述】:
我正在尝试对 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,因此我们将不胜感激!
谢谢!
米夏
【问题讨论】:
-
您要设置的颜色的示例是什么?它们都是独一无二的,还是您都是从一种颜色生成的?
-
我需要使用的颜色如下:
QColor color[3]; color[0].setHsv(249, 249, 249); color[1].setHsv(181, 181, 181); color[2].setHsv(214, 214, 214);最后一个颜色和第一个一样,所以顺序需要是:color[0], color[1], color[2 ],颜色[0]。 -
通过修改QSS来做动画是个坏主意。因为它很慢。
-
将一种颜色淡入另一种颜色效果很好。但是,如果您有其他方法,请随时分享!一直在寻找更好的解决方案。
标签: c++ qt qpushbutton qpropertyanimation qcolor