
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include #define PI 3.1415926QT_BEGIN_NAMESPACEnamespace Ui { class MainWindow; }QT_END_NAMESPACEclass MainWindow : public QMainWindow{ Q_OBJECTpublic: MainWindow(QWidget *parent = nullptr); ~MainWindow(); // QObject interfaceprotected: void timerEvent(QTimerEvent *event); // QWidget interfaceprotected: void paintEvent(QPaintEvent *event);private: int m_nSize = 10; float m_nT = 0;};#endif // MAINWINDOW_H
#include #include #include #include "mainwindow.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ resize(600, 600); startTimer(200);}MainWindow::~MainWindow(){}void MainWindow::timerEvent(QTimerEvent *event){ if (m_nT > 500) { m_nT = 0; } if (m_nSize == 5) { m_nSize = 10; } m_nSize--; m_nT += 1; update();}void MainWindow::paintEvent(QPaintEvent *event){ QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); painter.translate(this->width() / 2.0, this->height() / 2.0); painter.setPen(Qt::red); painter.setBrush(Qt::red); double t = 0; double vt = 0.01; double maxT = 2 * PI; int maxC = ceil(maxT / vt); QPainterPath path; double x = 0; double y = 0; QPointF pt(x, y); path.moveTo(pt); for (int i = 0; i < maxC; ++i) { x = 16 * pow(sin(t) ,3); y = 13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t); pt.setX(x * m_nSize); pt.setY(-y * m_nSize); t += vt; path.lineTo(pt); } painter.drawPath(path);}