Qt 6.3 五彩流星雨制作
思路
首先定义一个五角星数组, 然后使用QPainter进行定时绘制
model/Items.h
#include <QString>
#ifndef UNTITLED1_TABLEITEMS_H
#define UNTITLED1_TABLEITEMS_H
struct Pantagram {
int x;
int y;
qreal step;
QColor color;
int rotation;
qreal size;
};
#endif
ui/pentagram.h
#ifndef UNTITLED1_PENTAGRAM_H
#define UNTITLED1_PENTAGRAM_H
#include <QPushButton>
#include <QList>
#include <QRandomGenerator>
#include "model/TableItems.h"
class PentagramWidget : public QWidget {
Q_OBJECT
public:
explicit PentagramWidget(QWidget *parent = nullptr);
~PentagramWidget() override;
signals:
protected:
void paintEvent(QPaintEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
public:
QSize sizeHint() const override;
private:
QList<Pantagram> *m_data;
QRandomGenerator *random;
void drawSnakeflow(QPainter &painter);
};
#endif
ui/pentagram.cpp
#include "pentagram.h"
#include <QMouseEvent>
#include <QPainter>
#include <QRandomGenerator>
#include <QTimer>
#include <QPainterPath>
#define WIDTH 1000
#define HEIGHT 800
#define PANTAGRAM_COUNT 100
#define PANTAGRAM_MIN_SIZE 10
#define PANTAGRAM_MAX_SIZE 40
PentagramWidget::PentagramWidget(QWidget *parent) :
QWidget(parent) {
m_data = new QList<Pantagram>(PANTAGRAM_COUNT);
random = QRandomGenerator::global();
resize(WIDTH, HEIGHT);
setStyleSheet("background-color: black;");
setWindowTitle("Pentagram");
for (int i = 0; i < m_data->size(); ++i) {
m_data->operator[](i).x = random->bounded(0, WIDTH);
m_data->operator[](i).y = random->bounded(0, HEIGHT);
m_data->operator[](i).step = random->bounded(2, 8);
m_data->operator[](i).rotation = random->bounded(0, 360);
m_data->operator[](i).size = random->bounded(PANTAGRAM_MIN_SIZE, PANTAGRAM_MAX_SIZE);
m_data->operator[](i).color = QColor(random->bounded(0, 255),
random->bounded(0, 255),
random->bounded(0, 255));
}
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, qOverload<>(&PentagramWidget::update));
timer->start(100);
}
PentagramWidget::~PentagramWidget() {
delete (m_data);
}
void PentagramWidget::paintEvent(QPaintEvent *event) {
QPainter painter(this);
// 设置抗锯齿
painter.setRenderHint(QPainter::Antialiasing);
drawSnakeflow(painter);
}
void PentagramWidget::drawSnakeflow(QPainter &painter) {
// 遍历所有五角星
for (int i = 0; i < m_data->size(); ++i) {
Pantagram &pantagram = m_data->operator[](i);
// 画五角星
painter.setPen(QPen(pantagram.color, 1));
// 绘制五角星
painter.save();
painter.translate(pantagram.x, pantagram.y);
painter.rotate(pantagram.rotation);
// 计算正五角星的十个点
// y轴镜像
painter.scale(1, -1);
// 计算公式网站 https://zhidao.baidu.com/question/2073567152212492428/answer/1566351173.html
double r = pantagram.size;
QPointF A(r * cos(18 * M_PI / 180), r * sin(18 * M_PI / 180));
QPointF B(r * cos(90 * M_PI / 180), r * sin(90 * M_PI / 180));
QPointF C(r * cos(162 * M_PI / 180), r * sin(162 * M_PI / 180));
QPointF D(r * cos(234 * M_PI / 180), r * sin(234 * M_PI / 180));
QPointF E(r * cos(306 * M_PI / 180), r * sin(306 * M_PI / 180));
double r1 = r * sin(18 * M_PI / 180) / sin(126 * M_PI / 180);
QPointF F(r1 * cos(54 * M_PI / 180), r1 * sin(54 * M_PI / 180));
QPointF G(r1 * cos(126 * M_PI / 180), r1 * sin(126 * M_PI / 180));
QPointF H(r1 * cos(198 * M_PI / 180), r1 * sin(198 * M_PI / 180));
QPointF I(r1 * cos(270 * M_PI / 180), r1 * sin(270 * M_PI / 180));
QPointF J(r1 * cos(342 * M_PI / 180), r1 * sin(342 * M_PI / 180));
// 画五角星
QPainterPath path;
path.moveTo(B);
path.lineTo(F);
path.lineTo(A);
path.lineTo(J);
path.lineTo(E);
path.lineTo(I);
path.lineTo(D);
path.lineTo(H);
path.lineTo(C);
path.lineTo(G);
path.lineTo(B);
painter.drawPath(path);
// 透明度
painter.setOpacity(0.6);
painter.fillPath(path, QBrush(pantagram.color));
painter.restore();
// 移动五角星
pantagram.y += pantagram.step;
if (pantagram.y > height()) {
pantagram.y = 0;
}
pantagram.rotation = (pantagram.rotation + (int)pantagram.step) % 360;
}
}
void PentagramWidget::resizeEvent(QResizeEvent *event) {
// 重新计算五角星的位置
// resize的比例
double ratioW = (double) event->size().width() / event->oldSize().width();
double ratioH = (double) event->size().height() / event->oldSize().height();
if (ratioH < 0 || ratioW < 0) {
return;
}
for (int i = 0; i < m_data->size(); ++i) {
m_data->operator[](i).x *= ratioW;
m_data->operator[](i).y *= ratioH;
m_data->operator[](i).step *= ratioH;
}
}
QSize PentagramWidget::sizeHint() const {
return QSize(WIDTH, HEIGHT);
}
main.cpp
#include <QApplication>
#include <ui/pentagram.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
PentagramWidget pentagram;
pentagram.show();
return a.exec();
}