Simple animation not working
-
Hello! I am learning animation framework. I try to work out simple program. Let's pretend that i have window. How can i animate width of this window?
I try this code:
@
Window::Window(QWidget *parent)
: QWidget(parent)
{
anim=new QPropertyAnimation(this,"width");
anim->setEasingCurve(QEasingCurve::InOutBack);
anim->setDuration(100);
anim->setStartValue(300);
anim->setEndValue(500);
anim->setLoopCount(-1); // loop never ends
anim->start();
}
@
but get this message in application output:
@
Starting F:\dev\win\2dpaint-build-desktop\release\2dpaint.exe...
QPropertyAnimation: you're trying to animate the non-writable property x of your QObject
QPropertyAnimation: you're trying to animate the non-writable property x of your QObject
F:\dev\win\2dpaint-build-desktop\release\2dpaint.exe exited with code 0
@ -
The error message is quite clear: you cannot change the property width via the property system of QObject. If you look at "QWidget's property width":http://doc.qt.nokia.com/4.7/qwidget.html#width-prop you will see, that there is only a getter, not a setter.
-
Thanks! This one works perfect
@
anim=new QPropertyAnimation(this,"geometry");
anim->setEasingCurve(QEasingCurve::OutInQuad);
anim->setDuration(2000);
anim->setStartValue(QRect(100,100,100,100));
anim->setEndValue(QRect(150,150,300,200));
anim->setLoopCount(-1);
anim->start();
@So if i have setter for property i can make animation. I have few more questions:
- If i inherit QObject and create my own class with some parameter (width for example) and create setter function, can i animate this parameter with animation framework?
- How can i create endless animation as a sequence of direct and reverse animation?
-
I worked out second question:
@
anim=new QPropertyAnimation(this,"geometry");
anim->setEasingCurve(QEasingCurve::OutInQuad);
anim->setDuration(3500);
anim->setStartValue(QRect(100,100,100,100));
anim->setKeyValueAt(1.0/2.0,QRect(150,150,300,200));
anim->setEndValue(QRect(100,100,100,100));
anim->setLoopCount(-1);
anim->start();
@
Now i want to understand animation of custom properties. So to animate them i need inherit QObject and declarate my properties using macro Q_PROPERTY, right?