下面的代码是QPropertyAnimation中的实例代码:
QPropertyAnimation *animation = newQPropertyAnimation(myWidget, "geometry");
animation->setDuration(10000);
animation->setStartValue(QRect(0, 0, 100,30));
animation->setEndValue(QRect(250, 250,100, 30));
animation->start();
参考这段代码,自己利用QLabel产生动画,代码如下,但是QLabel没有显示
QLabel *pL = new QLabel(this);
pL->setFixedSize(50, 50);
pL->setAutoFillBackground(true);
pL->setBackgroundRole(QPalette::Dark);
QPropertyAnimation *animation = newQPropertyAnimation(pL, "geometry");
animation->setDuration(10000);
animation->setStartValue(QRect(0,0, 50,50));
animation->setEndValue(QRect(300, 300,50, 50));
animation->start();
查看了网上的例子(链接http://jingyan.baidu.com/article/154b46315757b628ca8f4116.html),那是因为相关的控件还没有show,因此,修改的代码如下,即可显示
QLabel *pL = new QLabel(this);
pL->setFixedSize(50, 50);
pL->setAutoFillBackground(true);
pL->setBackgroundRole(QPalette::Dark);
pL->show();
QPropertyAnimation *animation = newQPropertyAnimation(pL, "geometry");
animation->setDuration(10000);
animation->setStartValue(QRect(0,0, 50,50));
animation->setEndValue(QRect(300, 300,50, 50));
animation->start();
还有一个问题,QPropertyAnimation构造函数中的”geometry”到底是怎么来的,琢磨了很久,应该是这样来的,在参考文档中,找到QWidget这一个类,然后找到setGeometry这个属性,然后文档会跳转到
所以,我猜想”geometry”应该就是对应一个类的Property,如下图