3

I am tired of searching !

i subclassed a Button from QPushbutton and set my QSS to it.the style is desired.

all i want is when the button is hover (enterevent happen) the button's color change over a specific time (for example 0.2 sec) not immediately (a soft color changing)

what should i do ?

*******Answer in PyQt4*********

class MyButton(QPushButton):
    def __init__(self):
        super(MyButton, self).__init__()
        self.setMinimumSize(80,50)
        self.setText('QPushButton')

    def getColor(self):
        return Qt.black

    def setColor(self, color):
        self.setStyleSheet("background-color: rgb({0}, {1}, {2});border:none;".format(color.red(), color.green(), color.blue()))

    color=QtCore.pyqtProperty(QColor, getColor, setColor)

    def enterEvent(self, event):
        global anim
        anim=QPropertyAnimation(self, "color")
        anim.setDuration(200)
        anim.setStartValue(QColor(216, 140, 230))
        anim.setEndValue(QColor(230, 230, 230))
        anim.start()

    def leaveEvent(self, event):
        self.setStyleSheet("background:none;")
IMAN4K
  • 1,265
  • 3
  • 24
  • 42

1 Answers1

5

One of the solutions is - QPropertyAnimation class. It does not support color change out of box, but since you have subclassed button already anyway - here is a sample code.

First - you would need to define new property in your class - right after Q_OBJECT macro. And getter and setter methods for this property, example below:

class AnimatedButton : public QPushButton
{

  Q_OBJECT
  Q_PROPERTY(QColor color READ color WRITE setColor)

public:
  AnimatedButton (QWidget *parent = 0)
  {
  }
  void setColor (QColor color){
    setStyleSheet(QString("background-color: rgb(%1, %2, %3);").arg(color.red()).arg(color.green()).arg(color.blue()));
  }
  QColor color(){
    return Qt::black; // getter is not really needed for now
  }
};

and then in your event handler, where you process enterEvent, you should do something like this -

// since it will be in event of button itself, change myButton to 'this'
QPropertyAnimation *animation = new QPropertyAnimation(myButton, "color");
animation->setDuration(200); // duration in ms
animation->setStartValue(QColor(0, 0, 0));
animation->setEndValue(QColor(240, 240, 240));
animation->start();

though you would probably want to make sure to not start new animation unless this one is finished, and make sure that you don't have memory leak by calling new again and again

Shf
  • 3,463
  • 2
  • 26
  • 42
  • Thanks for reply.i did everything you said but it seems there is no different.i appreciate it if you verify my edit.thanks again – IMAN4K Dec 28 '15 at 18:38
  • @IMAN4K - you variable `anim` is not global and is destroyed as soon as method is executed. I added line `global anim` before `anim=QPropertyAnimation(self, "color")` and it was animated correctly – Shf Dec 28 '15 at 19:33
  • @IMAN4K and thanks to you i've finally set up pyqt on my ubuntu machine. Now i can start to learn python also, not c++ only :D – Shf Dec 29 '15 at 01:05
  • That sounds great ;) – IMAN4K Dec 29 '15 at 06:00