如何在 qt 中对 QPushButton 的背景颜色进行动画处理

How to animate the background color of a QPushButton in qt?

本文关键字:颜色 动画 处理 背景 qt 中对 QPushButton      更新时间:2023-10-16

我做了一切[直到我知道的]来动画按钮的背景颜色,但我做不到。以下是我的尝试之一:

    # I set the style of the button by setstylesheet()
    animation = QPropertyAnimation(btn, "style",btn)
    animation.setDuration(1000)
    animation.setStartValue(QStyle("background-color:blue"))
    animation.setEndValue(QStyle("background-color:red"))
    animation.start()

但以下代码显示以下错误:

animation.setStartValue(QStyle("background-color:blue"))TypeError: PyQt4.QtGui.QStyle 表示一个C++抽象类,无法实例化

我还尝试设置调色板:

    color = install_btn.palette()
    color.setColor(QPalette.Base,QColor(72, 152, 219))
    install_btn.setAutoFillBackground(True)
    install_btn.setPalette(color)

然后我没有在上面的动画值中QStyle,我写了QColor(r,g,b)但这也不起作用,它说:

QPropertyAnimation:您正在尝试对 QObject 的不存在的属性样式进行动画处理

请用您知道的任何语言(C++或Python)帮助我。但我使用Python。

谢谢是特别的

C++,Qt5,但可以在Qt4中工作

QGraphicsColorizeEffect *eEffect= new QGraphicsColorizeEffect(btn);
btn->setGraphicsEffect(eEffect);
QPropertyAnimation *paAnimation = new QPropertyAnimation(eEffect,"color");
paAnimation->setStartValue(QColor(Qt::blue));
paAnimation->setEndValue(QColor(Qt::red));
paAnimation->setDuration(1000);
paAnimation->start();