Animation problems
-
Hi
I am trying to animate the movement of one of my widgets
I have a simple Qt app consists of a main window with a label (done in QDesigner)
The code to move the label is
@
QPropertyAnimation animation(ui.label,"geometry");
animation.setDuration(10000);
animation.setStartValue(ui.label->rect());
QRect end = QRect(ui.label->rect().x() - 10,ui.label->y() - 10,ui.label->rect().width(),ui.label->rect().height());
animation.setEndValue(end);
animation.start();
@
Now this does move the label but it is move instantly whereas I thought that by specifying a duration , this would be how long it took to do the animation.I would be grateful if some could explain what I am doing wrong
Thanks
-
instead of creating the animation on the stack you should create it on the heap and specify that the animation should delete itself once it is finished:
@
QPropertyAnimation *animation = new QPropertyAnimation(ui.label,"geometry");
animation->setDuration(10000);
animation->setStartValue(ui.label->rect());
QRect end = QRect(ui.label->rect().x() - 10,ui.label->y() - 10,ui.label->rect().width(),ui.label->rect().height());
animation->setEndValue(end);
animation->start(QAbstractAnimation::DeleteWhenStopped);
@ -
or do it static:
@static QPropertyAnimation animation(ui->pushButtonRadio, "geometry");@
-
[quote author="NicuPopescu" date="1385392921"]or do it static:
@static QPropertyAnimation animation(ui->pushButtonRadio, "geometry");@[/quote]
i wouldn't do this, because it's a pitfall !!You do create a static QPropertyAnimation instance with a target object (of the first caller).
So if you use 2 instances of the same widget and this code gets called, the second instance will operate on the target object of the first. And also it will become invalid when the first instance gets destroyed and thus it will crash.
It would work if you don't specify the target object with the construction. But then you have absolutely no benefit from declaring it static ... -
bq. But then you have absolutely no benefit from declaring it static …
except it will function :) ... this was on my purpose -
i never said that it won't work... i explained that it has drawbacks...
You can write tons of stupid code that will work for a particular case but that shouldn't be the goal right?
And declaring it static like you suggested will only work flawlessly if you use the animation in a single instance of the class. -
pff!
bq. I am trying to animate the movement of one of my widgets
no many instances generic code in the context etc.
as it was explained by you someone could think is the only solution for getting QPropertyAnimation working, by allocating on heap ... or better to explain why does QPropertyAnimation not work as local variable!? that would enrich us indeed
-
and once more... hope you get it now:
[quote author="raven-worx" date="1385635372"]i never said that it won't work... i explained that it has drawbacks...[/quote]
hey i don't mind what anyone does in his code. It was an advice from me take it or leave it.and btw. just by reading the line "I am trying to animate the movement of one of my widgets " you know the widget structure of his application? If he wants to animate a child-widget of a widget he could have instantiated multiple times?