PDA

View Full Version : QPropertyAnimation does not redraw the object



Idefix82
12th June 2014, 03:10
These are my very first steps in Qt. I am trying to create a simple animation (under Ubuntu, Qt 4.8), and I don't think I am setting things up correctly.

I have a class MyWidget, inherited from QWidget:


class MyWidget : public QWidget
{
Q_OBJECT
Q_PROPERTY(double rmin READ rmin WRITE setmin)

public:
MyWidget();
double rmin() const;
void setmin(double m);
...

protected:
void paintEvent(QPaintEvent *event);
signals:

public slots:
private:
double p_rmin;
...
};

The getter and setter functions for the property are


double MyWidget::rmin() const{
return p_rmin;
}

void MyWidget::setmin(double m){
p_rmin=m;
}


The function
void MyWidget::paintEvent(QPaintEvent *event) uses the variable p_rmin to draw something (it access the variable only through the getter). Finally, my main() is:



int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget w;
w.show();

QPropertyAnimation* animation = new QPropertyAnimation(&w, "rmin");
animation->setDuration(5000);

animation->setStartValue(2.5);
animation->setEndValue(3.3);

animation->start(QAbstractAnimation::DeleteWhenStopped);
return a.exec();
}


When I run this, I only see the static picture that was drawn for the starting value of p_rmin (which was also initialised by the constructor of MyWidget). But every time I resize the window, or switch between windows, and thereby force a redraw, I see a picture for a corresponding intermediate value. If I include a call to update() in the property setter, then the animation works. Surely, the animate object should be forcing a redraw if I had set up things correctly? Many thanks in advance!

wysota
12th June 2014, 06:38
No, the animation only makes a property change. It is up to the property setter to inform the widget that the new value of the property requires the widget to be updated. Therefore your initial approach of adding an update() call to setmin() was correct.