1、我们选择动画Qt属性的一个主要理由是Qt属性为我们提供了自己动画已存在的类的自由度。尤其是QWidget类(我们也可以把它嵌入到一个QGraphicsView中)具有很多属性表示其bounds,colors等等。让我们看一个小例子:
QPushButton button("Animated Button");
button.show();
QPropertyAnimation animation(&button, "geometry");
animation.setDuration(10000);
animation.setStartValue(QRect(0, 0, 0, 0));
animation.setEndValue(QRect(250, 250, 100, 30));
animation.start();
这段代码将把按钮在10秒种内从屏幕的左上角移动到(250,250)处,而且是逐渐变大。见下图效果:
2、上面的例子举在开始值和结束值之间做线性插值。还可以在开始和结束值之间设置值,插值运算就会经过这些点。
animation1 = new QPropertyAnimation(ui.pushButton, "geometry");
animation1->setDuration(10000);
animation1->setKeyValueAt(0, QRect(0, 0, 00, 00));
animation1->setKeyValueAt(0.4, QRect(20, 250, 20, 30));
animation1->setKeyValueAt(0.8, QRect(100, 250, 20, 30));
animation1->setKeyValueAt(1, QRect(250, 250, 100, 30));
animation1->setEndValue(QRect(250, 250, 100, 30));
在此例中,动画将按钮在8秒中内弄到(250,250)处,然后在2秒种内又弄回原位。移位是在这些点中间以线性插值进行的。
3、你也有可能动画一个QObject的值,虽然这些值并没有被声明为Qt属性。唯一的要求就是这个值具有一个setter。之后你可以从这个类派生子类从而包含这些值并且声明一个使用这个setter的属性。注意每个Qt属性都需要有一个getter,所以你需要提供一个getter,如果它不存在的话。
class MyGraphicsRectItem : public QObject, public QGraphicsRectItem
{
Q_OBJECT
Q_PROPERTY(QRectF geometry READ geometry WRITE setGeometry)
};
在上例中,我们派生了QGraphicsRectItem并定义了一个geometry属性。我们现在可以动画这个widget的geometry了,即使QGraphicsRectItem没有提供geometry属性。
-
动画和图形视图框架
当你想动画QGraphicsItems,你也要用QPropertyAnimation。然而,QGraphicsItem不是从QObject派生的。一个好的解决方案是派生要动画的图形item。派生类也要从QObject派生。这样,QPropertyAnimation就可以被用于QGraphicsItems了。
class Pixmap : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
Q_PROPERTY(QPointF pos READ pos WRITE setPos)
...
就如上一节中所讲的,我们需要定义希望去动画的属性。
注意:QObject必须是继承中的第一个,因为元数据对象系统需要这样做。
-
宽松曲线
QPropertyAnimation在属性的开始值和结束值之间执行一个插值运算。除了向动画添加更多的关键值外,你还可以使用一个宽松曲线。宽松曲线描述了一个在0和1之间插值的速度变化的函数,如果你想控制一个动画的速度而不改变插值的路径时,就非常有用。
animation1 = new QPropertyAnimation(ui.pushButton, "geometry");
animation1->setDuration(10000);
animation1->setStartValue(QRect(0, 0, 0, 0));
animation1->setEndValue(QRect(250, 250, 100, 30));
animation1->setEasingCurve(QEasingCurve::OutBounce);
这里,动画将按照一个曲线进行,这个曲线使得动画像一个跳动的皮球从开始位置跳到结束位置。QEasingCurve具有一个大曲线集合,你可以从里面选择一个。它们被定义为QEasingCurve::Type枚举。如果你需要不一样的曲线,你也可以自己实现一个,然后注册到QEasingCurve。
-
错误1:QPropertyAnimation: you're trying to animate a non-existing property 属性窗口
解释:
animation1 = new QPropertyAnimation(ui.pushButton, "geometry");
估计是你给的属性名字(propertyName)是错误的,必须是qt自带的属性名字,或者你通过自定义实现的名字。
我要是把代码写成:
animation1 = new QPropertyAnimation(ui.pushButton, "geometry23");
会提示如下错误,找不到属性geometry23
QPropertyAnimation: you're trying to animate a non-existing property setGeometry23 of your QObject
http://jingyan.baidu.com/article/154b46315757b628ca8f4116.html