简体   繁体   中英

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

I did everything [till what i know] to animate the background color of a button but i can't do it. Here are one of my tries:

    # 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()

But the following code shows the following error:

animation.setStartValue(QStyle("background-color:blue")) TypeError: PyQt4.QtGui.QStyle represents a C++ abstract class and cannot be instantiated

I also tried to set a palette:

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

and then instead of QStyle in above animation value i wrote QColor(r,g,b) but that too didn't worked, it said:

QPropertyAnimation: you're trying to animate a non-existing property style of your QObject

Please help me in any language you know (C++ or Python). But i use Python.

Thanks is special

C++, Qt5, but may work in 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM