当前位置:   article > 正文

Qt动画效果-【初学一】_stackedwidget 动画切换

stackedwidget 动画切换

一、需要的头文件

  1. #include <QLabel>
  2. #include <QPropertyAnimation>
  3. #include <QParallelAnimationGroup>

二、窗口动画效果

1.软件启动动画效果

  1. //桌面宽度
  2. int CommonlyUsed::deskWidth()
  3. {
  4. return qApp->desktop()->availableGeometry().width();
  5. }
  6. //桌面高度
  7. int CommonlyUsed::deskHeight()
  8. {
  9. return qApp->desktop()->availableGeometry().height();
  10. }
  11. //软件窗口渐进开启
  12. void MainWindow::Software_startup_animation()
  13. {
  14. //界面动画,窗口右移
  15. QPropertyAnimation *anim1 = new QPropertyAnimation(this,"pos");
  16. anim1->setDuration(800); //动画持续时间
  17. anim1->setStartValue(QPoint(deskWidth()/2 - this->width(),deskHeight()/2 - this->height()/2));
  18. anim1->setEndValue(QPoint(deskWidth()/2 - this->width()/2,deskHeight()/2 - this->height()/2));
  19. anim1->setEasingCurve(QEasingCurve::Linear); //设置速度曲线
  20. //界面动画,渐显效果
  21. QPropertyAnimation *anim2 = new QPropertyAnimation(this, "windowOpacity");
  22. anim2 ->setDuration(800); //动画持续时间
  23. anim2 ->setStartValue(0); //设置初始值
  24. anim2 ->setEndValue(1); //设置结束值(0-1,透明度由完全透明→不透明)
  25. //创建并行动画组
  26. QParallelAnimationGroup *group = new QParallelAnimationGroup(this);
  27. group->addAnimation(anim1); //动画组移动动画
  28. group->addAnimation(anim2); //动画组添加透明-显现动画
  29. group->start(QAbstractAnimation::DeleteWhenStopped); //动画组中的动画执行完毕后删除对象
  30. }
  31. //界面动画,窗口下降
  32. void MainWindow::Window_down()
  33. {
  34. QPropertyAnimation *anim1 = new QPropertyAnimation(this,"pos");
  35. anim1->setDuration(800);
  36. anim1->setStartValue(QPoint(this->x(),this->y() - this->height()));
  37. anim1->setEndValue(QPoint(this->x(),this->y()));
  38. anim1->setEasingCurve(QEasingCurve::Linear); //设置速度曲线
  39. }
  40. //界面动画,窗口下坠
  41. void frmMain::onDropWindow()
  42. {
  43. QPropertyAnimation *pAnimation = new QPropertyAnimation(this, "geometry");
  44. QDesktopWidget *pDesktopWidget = QApplication::desktop();
  45. int x = (pDesktopWidget->availableGeometry().width() - width()) / 2;
  46. int y = (pDesktopWidget->availableGeometry().height() - height()) / 2;
  47. pAnimation->setDuration(1500);
  48. pAnimation->setStartValue(QRect(x, 0, width(), height()));
  49. pAnimation->setEndValue(QRect(x, y, width(), height()));
  50. pAnimation->setEasingCurve(QEasingCurve::OutElastic);
  51. pAnimation->start(QAbstractAnimation::DeleteWhenStopped);
  52. }
  53. //窗口抖动动画
  54. void MainWindow::ShakeAnimation()
  55. {
  56. //设置软件在桌面的中心位置
  57. this->setGeometry(qApp->desktop()->availableGeometry().width()/2 - this->width()/2,
  58. qApp->desktop()->availableGeometry().height()/2 - this->height()/2,
  59. this->width(),this->height());
  60. m_animation = new QPropertyAnimation(this,"pos");
  61. //获取坐标
  62. QPoint pos = this->pos();
  63. //动画还没有结束就先立马停止,防止用户不停的点击
  64. if(m_animation->state() == QPropertyAnimation::Running)
  65. {
  66. m_animation->stop();
  67. }
  68. m_animation->setDuration(600);
  69. m_animation->setStartValue(pos);
  70. m_animation->setKeyValueAt(0.1,pos + QPoint(-12,-12));
  71. m_animation->setKeyValueAt(0.2,pos + QPoint(0,-4));
  72. m_animation->setKeyValueAt(0.3,pos + QPoint(4,0));
  73. m_animation->setKeyValueAt(0.4,pos + QPoint(4,2));
  74. m_animation->setKeyValueAt(0.5,pos + QPoint(6,-6));
  75. m_animation->setKeyValueAt(0.6,pos + QPoint(-4,4));
  76. m_animation->setKeyValueAt(0.7,pos + QPoint(-4,0));
  77. m_animation->setKeyValueAt(0.8,pos + QPoint(0,8));
  78. m_animation->setKeyValueAt(0.9,pos + QPoint(12,12));
  79. m_animation->setEndValue(pos);
  80. m_animation->start();
  81. }

2.软件关闭动画效果

  1. //关闭动画
  2. void MainWindow::closeMainInterface()
  3. {
  4. //界面动画,窗口下坠
  5. QPropertyAnimation* closeAnimation = new QPropertyAnimation(this,"geometry");
  6. closeAnimation->setStartValue(geometry());
  7. closeAnimation->setEndValue(QRect(geometry().x(),geometry().y()+height(),width(),0));
  8. closeAnimation->setDuration(600);
  9. closeAnimation->setEasingCurve(QEasingCurve::Linear); //设置速度曲线
  10. //界面动画,消失效果
  11. QPropertyAnimation *WindowDisappear_animation = new QPropertyAnimation(this, "windowOpacity");
  12. WindowDisappear_animation->setDuration(600);
  13. WindowDisappear_animation->setStartValue(1);
  14. WindowDisappear_animation->setEndValue(0);
  15. //创建并行动画组
  16. QParallelAnimationGroup *group = new QParallelAnimationGroup(this);
  17. group->addAnimation(closeAnimation); //动画组添加移动动画
  18. group->addAnimation(WindowDisappear_animation); //动画组添加透明-显现动画
  19. group->start(QAbstractAnimation::DeleteWhenStopped); //动画组中的动画执行完毕后删除对象
  20. connect(group, &QParallelAnimationGroup::finished, this, &MainWindow::myCloseWindow); //关闭动画完成后再退出软件
  21. }
  22. //关闭
  23. void MainWindow::myCloseWindow()
  24. {
  25. QApplication* app;
  26. app->exit(0);
  27. }

1.代码解释:

anim1->setEasingCurve(QEasingCurve::Linear);

 三、stackedWidget界面切换的简单动画效果

  1. //界面切换动画
  2. void MainWindow::Interface_Switching_Animation(int m_currentIndex)
  3. {
  4. #if 0
  5. //截取整个图片并向右边移动
  6. {
  7. *QLabel *m_label = new QLabel(ui->stackedWidget);
  8. m_label->resize(QSize(ui->stackedWidget->width(),ui->stackedWidget->height()));
  9. m_label->setPixmap(ui->stackedWidget->grab()); //捕获当前界面并绘制到label上
  10. m_label->setAttribute(Qt::WA_DeleteOnClose); //设置属性(关闭时删除)
  11. m_label->show();
  12. QPropertyAnimation *animation1 = new QPropertyAnimation(m_label,"geometry");
  13. animation1->setDuration(1000); //设置动画时间为1秒
  14. animation1->setStartValue(QRect(0,0,ui->stackedWidget->width(),ui->stackedWidget->height()));
  15. animation1->setEndValue(QRect(ui->stackedWidget->width()*2,0,ui->stackedWidget->width(),ui->stackedWidget->height()));
  16. animation1->setEasingCurve(QEasingCurve::InCubic); //设置速度曲线(先慢后快)
  17. QParallelAnimationGroup *group = new QParallelAnimationGroup; //并行动画容器(这里只用了一个动画,可以自己添加动画后再添加到并行动画容器中实现多动画效果)
  18. group->addAnimation(animation1);
  19. group->start(QAbstractAnimation::DeleteWhenStopped);
  20. ui->stackedWidget->setCurrentIndex(m_currentIndex); //切换到对应的界面
  21. m_label->raise(); //把label置顶显示
  22. connect(group, &QParallelAnimationGroup::finished, this, [=](){m_label->close();}); //关闭动画完成后关闭label
  23. }
  24. #endif
  25. }
  26. #if 1
  27. //开门动画
  28. {
  29. QLabel *m_label = new QLabel(ui->stackedWidget_matrix);
  30. m_label->resize(QSize(ui->stackedWidget_matrix->width()/2,ui->stackedWidget_matrix->height()));
  31. m_label->setAttribute(Qt::WA_DeleteOnClose);
  32. //裁剪截取到的图片,只复制图片左边一半
  33. QPixmap pix = ui->stackedWidget_matrix->grab();
  34. QPixmap pixleft = pix.copy(0,0,ui->stackedWidget_matrix->width()/2,ui->stackedWidget_matrix->height());
  35. m_label->setPixmap(pixleft); //捕获当前界面并绘制到label上
  36. m_label->show();
  37. QLabel *m_label1 = new QLabel(ui->stackedWidget_matrix);
  38. m_label1->resize(QSize(ui->stackedWidget_matrix->width()/2,ui->stackedWidget_matrix->height()));
  39. m_label1->setAttribute(Qt::WA_DeleteOnClose);
  40. //裁剪截取到的图片,只复制图片右边边一半
  41. QPixmap pixright = pix.copy(ui->stackedWidget_matrix->width()/2,0,ui->stackedWidget_matrix->width()/2,ui->stackedWidget_matrix->height());
  42. m_label1->setPixmap(pixright); //捕获当前界面并绘制到label上
  43. m_label1->show();
  44. //左边图片向左移动
  45. QPropertyAnimation *animation1 = new QPropertyAnimation(m_label,"geometry");
  46. animation1->setDuration(1500); //设置动画时间为1.5秒
  47. animation1->setStartValue(QRect(0,0,ui->stackedWidget_matrix->width()/2,ui->stackedWidget_matrix->height()));
  48. animation1->setEndValue(QRect(-ui->stackedWidget_matrix->width()*2,0,ui->stackedWidget_matrix->width()/2,ui->stackedWidget_matrix->height()));
  49. animation1->setEasingCurve(QEasingCurve::InCubic); //设置速度曲线(先慢后快)
  50. //右边图片向右移动
  51. QPropertyAnimation *animation2 = new QPropertyAnimation(m_label1,"geometry");
  52. animation2->setDuration(1500); //设置动画时间为1.5秒
  53. animation2->setStartValue(QRect(ui->stackedWidget_matrix->width()/2,0,ui->stackedWidget_matrix->width()/2,ui->stackedWidget_matrix->height()));
  54. animation2->setEndValue(QRect(ui->stackedWidget_matrix->width()*2,0,ui->stackedWidget_matrix->width()/2,ui->stackedWidget_matrix->height()));
  55. animation2->setEasingCurve(QEasingCurve::InCubic); //设置速度曲线(先慢后快)
  56. QParallelAnimationGroup *group = new QParallelAnimationGroup; //并行动画容器
  57. group->addAnimation(animation1); //添加动画
  58. group->addAnimation(animation2);
  59. group->start(QAbstractAnimation::DeleteWhenStopped);
  60. ui->stackedWidget_matrix->setCurrentIndex(m_currentIndex);
  61. //把label置顶显示
  62. m_label->raise();
  63. m_label1->raise();
  64. connect(group, &QParallelAnimationGroup::finished, this, [=](){m_label->close();m_label1->close();}); //关闭动画完成后再退出软件
  65. }
  66. #endif

展示效果:

 

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/81492
推荐阅读
相关标签
  

闽ICP备14008679号