Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Заголовочный файл
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- #include <QVector>
- #include <QGraphicsItemAnimation>
- #include <QGraphicsScene>
- #include <QPushButton>
- #include <QVBoxLayout>
- QT_BEGIN_NAMESPACE
- namespace Ui { class MainWindow; }
- QT_END_NAMESPACE
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- MainWindow(QWidget *parent = nullptr);
- ~MainWindow();
- private slots:
- void startAnimation();
- void finishAnimation();
- private:
- // Methods
- void initialize();
- void initializeItemList();
- void setupAnimation();
- private:
- Ui::MainWindow *ui;
- // UI items
- QVBoxLayout* m_mainLayout;
- QPushButton* m_startButton;
- // Graphics view items
- // Animation and items
- QGraphicsItemAnimation* m_animation;
- QVector<QGraphicsItem*> m_itemList;
- // Scene, view e.t.c.
- QGraphicsScene* m_scene;
- QGraphicsView *m_view;
- QTimeLine* m_timer;
- };
- #endif // MAINWINDOW_H
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Сурс файл
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <QGraphicsEllipseItem>
- #include <QGraphicsView>
- #include <QRect>
- #include <QTimeLine>
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- , ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- showMaximized();
- // Inits for widgets
- m_startButton = new QPushButton(tr("Start animation"));
- m_startButton->setGeometry(QRect(QPoint(100, 100), QSize(200, 50)));
- connect(m_startButton, &QPushButton::pressed, this, &MainWindow::startAnimation);
- m_view = new QGraphicsView();
- m_view->setStyleSheet(
- "border-width: 2px; border-style: solid; border-color: darkblue; "
- );
- // Adding widgets to layout
- m_mainLayout = new QVBoxLayout(ui->centralwidget);
- m_mainLayout->addWidget(m_view);
- m_mainLayout->addWidget(m_startButton);
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
- void MainWindow::startAnimation()
- {
- initialize();
- if (m_itemList.size() != 0)
- {
- m_view->show();
- m_timer->start();
- }
- }
- void MainWindow::initialize()
- {
- initializeItemList();
- m_timer = new QTimeLine(5000);
- m_timer->setFrameRange(0, 100);
- setupAnimation();
- m_scene = new QGraphicsScene();
- m_scene->setSceneRect(0, 0, width() - 100, height() - 50);
- ui->centralwidget->showMinimized();
- for (auto item : m_itemList)
- {
- m_scene->addItem(item);
- }
- m_view->setScene(m_scene);
- connect(m_timer, &QTimeLine::finished, this, &MainWindow::finishAnimation);
- }
- void MainWindow::initializeItemList()
- {
- QGraphicsRectItem* redEllipse = new QGraphicsRectItem(width() / 2, height() / 2, 20, 20);
- redEllipse->setBrush(QBrush(QColor(Qt::red)));
- m_itemList.append(redEllipse);
- QGraphicsRectItem* blueEllipse = new QGraphicsRectItem(width() / 2, height() / 2, 20, 20);
- blueEllipse->setBrush(QBrush(QColor(Qt::blue)));
- m_itemList.append(blueEllipse);
- QGraphicsRectItem* greenEllipse = new QGraphicsRectItem(width() / 2, height() / 2, 20, 20);
- redEllipse->setBrush(QBrush(QColor(Qt::green)));
- m_itemList.append(greenEllipse);
- QGraphicsRectItem* yellowEllipse = new QGraphicsRectItem(width() / 2, height() / 2, 20, 20);
- blueEllipse->setBrush(QBrush(QColor(Qt::yellow)));
- m_itemList.append(yellowEllipse);
- }
- void MainWindow::setupAnimation()
- {
- for (int itemNumber = 0; itemNumber < m_itemList.size(); itemNumber++)
- {
- QGraphicsItemAnimation *itemAnimation = new QGraphicsItemAnimation();
- itemAnimation->setTimeLine(m_timer);
- itemAnimation->setItem(m_itemList[itemNumber]);
- if (itemNumber == 0)
- {
- for (int index = 0; index < 200; index++)
- {
- itemAnimation->setPosAt(index / 200.0, QPointF(-index, -index));
- }
- }
- if (itemNumber == 1)
- {
- for (int index = 0; index < 200; index++)
- {
- itemAnimation->setPosAt(index / 200.0, QPointF(index, index));
- }
- }
- if (itemNumber == 2)
- {
- for (int index = 0; index < 200; index++)
- {
- itemAnimation->setPosAt(index / 200.0, QPointF(-index, index));
- }
- }
- if (itemNumber == 3)
- {
- for (int index = 0; index < 200; index++)
- {
- itemAnimation->setPosAt(index / 200.0, QPointF(index, -index));
- }
- }
- }
- }
- void MainWindow::finishAnimation()
- {
- // Do smth when animation is finished
- }
Add Comment
Please, Sign In to add comment