edward4324

Animation Qt example

Jul 30th, 2022
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.13 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. // Заголовочный файл
  3. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4. #ifndef MAINWINDOW_H
  5. #define MAINWINDOW_H
  6.  
  7. #include <QMainWindow>
  8. #include <QVector>
  9. #include <QGraphicsItemAnimation>
  10. #include <QGraphicsScene>
  11. #include <QPushButton>
  12. #include <QVBoxLayout>
  13.  
  14. QT_BEGIN_NAMESPACE
  15. namespace Ui { class MainWindow; }
  16. QT_END_NAMESPACE
  17.  
  18. class MainWindow : public QMainWindow
  19. {
  20.     Q_OBJECT
  21.  
  22. public:
  23.     MainWindow(QWidget *parent = nullptr);
  24.     ~MainWindow();
  25.  
  26. private slots:
  27.     void startAnimation();
  28.     void finishAnimation();
  29.  
  30. private:
  31.     // Methods
  32.     void initialize();
  33.     void initializeItemList();
  34.     void setupAnimation();
  35.  
  36. private:
  37.     Ui::MainWindow *ui;
  38.  
  39.     // UI items
  40.     QVBoxLayout* m_mainLayout;
  41.     QPushButton* m_startButton;
  42.  
  43.     // Graphics view items
  44.     // Animation and items
  45.     QGraphicsItemAnimation* m_animation;
  46.     QVector<QGraphicsItem*> m_itemList;
  47.  
  48.     // Scene, view e.t.c.
  49.     QGraphicsScene* m_scene;
  50.     QGraphicsView *m_view;
  51.     QTimeLine* m_timer;
  52. };
  53. #endif // MAINWINDOW_H
  54.  
  55. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  56. // Сурс файл
  57. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  58.  
  59. #include "mainwindow.h"
  60. #include "ui_mainwindow.h"
  61.  
  62. #include <QGraphicsEllipseItem>
  63. #include <QGraphicsView>
  64. #include <QRect>
  65. #include <QTimeLine>
  66.  
  67. MainWindow::MainWindow(QWidget *parent)
  68.     : QMainWindow(parent)
  69.     , ui(new Ui::MainWindow)
  70. {
  71.     ui->setupUi(this);
  72.     showMaximized();
  73.  
  74.     // Inits for widgets
  75.     m_startButton = new QPushButton(tr("Start animation"));
  76.     m_startButton->setGeometry(QRect(QPoint(100, 100), QSize(200, 50)));
  77.     connect(m_startButton, &QPushButton::pressed, this, &MainWindow::startAnimation);
  78.  
  79.     m_view = new QGraphicsView();
  80.     m_view->setStyleSheet(
  81.                 "border-width: 2px; border-style: solid; border-color: darkblue; "
  82.                 );
  83.  
  84.     // Adding widgets to layout
  85.     m_mainLayout = new QVBoxLayout(ui->centralwidget);
  86.     m_mainLayout->addWidget(m_view);
  87.     m_mainLayout->addWidget(m_startButton);
  88. }
  89.  
  90. MainWindow::~MainWindow()
  91. {
  92.     delete ui;
  93. }
  94.  
  95. void MainWindow::startAnimation()
  96. {
  97.     initialize();
  98.  
  99.     if (m_itemList.size() != 0)
  100.     {
  101.         m_view->show();
  102.         m_timer->start();
  103.     }
  104. }
  105.  
  106. void MainWindow::initialize()
  107. {
  108.     initializeItemList();
  109.  
  110.     m_timer = new QTimeLine(5000);
  111.     m_timer->setFrameRange(0, 100);
  112.  
  113.     setupAnimation();
  114.  
  115.     m_scene = new QGraphicsScene();
  116.     m_scene->setSceneRect(0, 0, width() - 100, height() - 50);
  117.     ui->centralwidget->showMinimized();
  118.     for (auto item : m_itemList)
  119.     {
  120.         m_scene->addItem(item);
  121.     }
  122.  
  123.     m_view->setScene(m_scene);
  124.  
  125.     connect(m_timer, &QTimeLine::finished, this, &MainWindow::finishAnimation);
  126. }
  127.  
  128. void MainWindow::initializeItemList()
  129. {
  130.     QGraphicsRectItem* redEllipse = new QGraphicsRectItem(width() / 2, height() / 2, 20, 20);
  131.     redEllipse->setBrush(QBrush(QColor(Qt::red)));
  132.     m_itemList.append(redEllipse);
  133.  
  134.     QGraphicsRectItem* blueEllipse = new QGraphicsRectItem(width() / 2, height() / 2, 20, 20);
  135.     blueEllipse->setBrush(QBrush(QColor(Qt::blue)));
  136.     m_itemList.append(blueEllipse);
  137.  
  138.     QGraphicsRectItem* greenEllipse = new QGraphicsRectItem(width() / 2, height() / 2, 20, 20);
  139.     redEllipse->setBrush(QBrush(QColor(Qt::green)));
  140.     m_itemList.append(greenEllipse);
  141.  
  142.     QGraphicsRectItem* yellowEllipse = new QGraphicsRectItem(width() / 2, height() / 2, 20, 20);
  143.     blueEllipse->setBrush(QBrush(QColor(Qt::yellow)));
  144.     m_itemList.append(yellowEllipse);
  145. }
  146.  
  147. void MainWindow::setupAnimation()
  148. {
  149.     for (int itemNumber = 0; itemNumber < m_itemList.size(); itemNumber++)
  150.     {
  151.         QGraphicsItemAnimation *itemAnimation = new QGraphicsItemAnimation();
  152.         itemAnimation->setTimeLine(m_timer);
  153.         itemAnimation->setItem(m_itemList[itemNumber]);
  154.         if (itemNumber == 0)
  155.         {
  156.             for (int index = 0; index < 200; index++)
  157.             {
  158.                 itemAnimation->setPosAt(index / 200.0, QPointF(-index, -index));
  159.             }
  160.         }
  161.         if (itemNumber == 1)
  162.         {
  163.             for (int index = 0; index < 200; index++)
  164.             {
  165.                 itemAnimation->setPosAt(index / 200.0, QPointF(index, index));
  166.             }
  167.         }
  168.         if (itemNumber == 2)
  169.         {
  170.             for (int index = 0; index < 200; index++)
  171.             {
  172.                 itemAnimation->setPosAt(index / 200.0, QPointF(-index, index));
  173.             }
  174.         }
  175.         if (itemNumber == 3)
  176.         {
  177.             for (int index = 0; index < 200; index++)
  178.             {
  179.                 itemAnimation->setPosAt(index / 200.0, QPointF(index, -index));
  180.             }
  181.         }
  182.     }
  183. }
  184.  
  185. void MainWindow::finishAnimation()
  186. {
  187.     // Do smth when animation is finished
  188. }
  189.  
  190.  
Add Comment
Please, Sign In to add comment