QPropertyAnimation 学习笔记7

https://blog.csdn.net/zx249388847/article/details/54864669

QPropertyAnimation  用于产生动画效果。

 
  1. QPropertyAnimation *animation = new QPropertyAnimation(myWidget, "geometry");

  2. animation->setDuration(10000);

  3. animation->setStartValue(QRect(0, 0, 100, 30));

  4. animation->setEndValue(QRect(250, 250, 100, 30));

  5.  
  6. animation->start();

这是文档中给出的例子,动画效果为,将窗口从0,0 位置缓慢移动到250,QPropertyAnimation 用来对Qt属性进行插值,Qt现在支持的QVariant 类型有QRect,QRectF,QLine,QLineF,QPoint,QColor,int ,double,float等。

这里为一个widget对象的geometry属性创建动画。

setDuration 设置动画时间,ms

setStartValue 设置开始属性

setEndValue 设置结束属性

start开始动画。

除了设置开始属性和结束属性外,还可以调用

void QVariantAnimation::setKeyValueAt(qreal step, const QVariant &value)

在动画中间设置属性值。取值范围为0.0-1.0,0开始,1结束。

其他使用参考文档。

这里将以一个例子说明如何在实战中使用:

这是360界面中的三个按钮,当鼠标进入或离开时,会有动画效果产生,博客中似乎不能上传动画,所以只能提供一张截图了。

代码很简单,也很容易懂,就不多说了。

 
  1. class mainButton : public QPushButton//用于主的图片

  2. {

  3. Q_OBJECT

  4. public:

  5. mainButton(QString pixnormal,QString pixenter,QString pixleave,QWidget*parent);

  6. ~mainButton();

  7. protected:

  8. void enterEvent(QEvent*);

  9. void leaveEvent(QEvent*);

  10. void paintEvent(QPaintEvent*event);

  11.  
  12. QPropertyAnimation*m_enteranimation;

  13. QPropertyAnimation*m_leaveanimation;

  14.  
  15. QList<QPixmap> m_enterlist;

  16. QList<QPixmap> m_leavelist;

  17. QPixmap m_pixnormal;

  18. int m_enterIndex;

  19. int m_leaveIndex;

  20. bool m_enter;

  21. bool m_leave;

  22. public slots:

  23. void entervaluechange(QVariant var){m_enterIndex=var.toInt();update();}

  24. void leavevaluechange(QVariant var){m_leaveIndex=var.toInt();update();}

  25. };

  26.  
  27.  
  28. mainButton::mainButton(QString strpixnormal,QString strpixenter,QString strpixleave,QWidget*parent):QPushButton(parent)

  29. {

  30. QPixmap pixnormal(strpixnormal);

  31. QPixmap pixenter(strpixenter);

  32. QPixmap pixleave(strpixleave);

  33.  
  34. setCursor(Qt::PointingHandCursor);

  35. m_leave=false;

  36. m_enter=true;

  37. m_leaveIndex=0;

  38. m_enterIndex=0;

  39. m_pixnormal=pixnormal;

  40. for(int i=0;i<10;i++)//进入

  41. {

  42. m_enterlist<<pixenter.copy(i*(pixenter.width()/10),0,pixenter.width()/10,pixenter.height());

  43. }

  44. for(int j=0;j<8;j++)//离开

  45. {

  46. m_leavelist<<pixleave.copy(j*(pixleave.width()/8),0,pixleave.width()/8,pixleave.height());

  47. }

  48. m_enteranimation=new QPropertyAnimation(this,"");

  49. m_enteranimation->setStartValue(0);

  50. m_enteranimation->setEndValue(9);

  51. m_enteranimation->setDuration(600);

  52. connect(m_enteranimation,SIGNAL(valueChanged(QVariant)),this,SLOT(entervaluechange(QVariant)));

  53.  
  54. m_leaveanimation=new QPropertyAnimation(this,"");

  55. m_leaveanimation->setStartValue(0);

  56. m_leaveanimation->setEndValue(7);

  57. m_leaveanimation->setDuration(600);

  58. connect(m_leaveanimation,SIGNAL(valueChanged(QVariant)),this,SLOT(leavevaluechange(QVariant)));

  59. }

  60. mainButton::~mainButton()

  61. {

  62. delete m_leaveanimation;

  63. delete m_enteranimation;

  64. }

  65. void mainButton::enterEvent(QEvent *)

  66. {

  67. m_enter=true;

  68. m_leave=false;

  69. m_enteranimation->start();

  70. }

  71. void mainButton::leaveEvent(QEvent *)

  72. {

  73. m_enter=false;

  74. m_leave=true;

  75. m_leaveanimation->start();

  76. }

  77. void mainButton::paintEvent(QPaintEvent *event)

  78. {

  79. QPainter painter(this);

  80. if(m_enter)

  81. painter.drawPixmap(rect(),m_enterlist.at(m_enterIndex));

  82. if(m_leave)

  83. painter.drawPixmap(rect(),m_leavelist.at(m_leaveIndex));

  84. }

猜你喜欢

转载自blog.csdn.net/wang13342322203/article/details/81976996