【问题标题】:Change the background color of a QPushButton with QPropertyAnimation使用 QPropertyAnimation 更改 QPushButton 的背景颜色
【发布时间】: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


【解决方案1】:

最简单的方法是为每个停靠点定义一个属性:

Q_PROPERTY(QColor color1 READ color1 WRITE setColor1)
Q_PROPERTY(QColor color2 READ color2 WRITE setColor2)
Q_PROPERTY(QColor color3 READ color3 WRITE setColor3)
Q_PROPERTY(QColor color4 READ color4 WRITE setColor4)

虽然很丑。

在您的评论中,您提到每种颜色的 RGB 分量是相同的,分量基本上是 249、181、214 和 249。如果您知道每种颜色都是基于 249,您可以这样做:

void setColor (QColor color){
    const int base = color.red();
    const int stop2 = (181.0 / 249.0) * base;
    const int stop3 = (214.0 / 249.0) * base;
    setStyleSheet(QString("QPushButton { background: qlineargradient(x1:0, y1:0, x2:0, y2:1,") +
                  QString("stop: 0    rgba(%1, %2, %3, 255),").arg( base ).arg( base ).arg( base ) +
                  QString("stop: 0.5  rgba(%1, %2, %3, 255),").arg( stop2 ).arg( stop2 ).arg( stop2 ) +
                  QString("stop: 0.6  rgba(%1, %2, %3, 255),").arg( stop3 ).arg( stop3 ).arg( stop3 ) +
                  QString("stop: 1    rgba(%1, %2, %3, 255),").arg( base ).arg( base ).arg( base ));
}

这允许您为color 属性使用任何颜色(只要所有组件都相同),其他颜色将自动缩放。

正如@SaZ 所提到的,以这种方式修改样式表可能会很慢。您可以考虑改用QPalette,或者(可能是最快的选项)覆盖paintEvent() 并自己绘制小部件。

【讨论】:

  • 感谢您的努力,米奇!但是由于我对 QPalette 或自定义paintEvents 都不熟悉,您能否提供一个示例?
  • 这是一个自定义 QWidget,其中 paintEvent() 被覆盖:gist.github.com/mitchcurtis/233f78d42d62e654f773
  • 嗯,抱歉,无法将其转移到我的问题... :(
  • 为样式表制作动画完全是矫枉过正。我永远不会那样做。
  • @MichaW。那么你需要改进你的问题,使其更具体。
猜你喜欢
  • 2014-01-07
  • 2014-08-30
  • 1970-01-01
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
  • 2016-04-02
  • 2012-07-10
  • 2016-04-11
相关资源
最近更新 更多