#pragma once #include #include #include #include #include #include #include #include "Background.h" #include "effect.h" #include "global.h" struct Wave : Paintable { QColor color = Qt::blue; QGraphicsDropShadowEffect shadow_effect; Wave() { shadow_effect.setBlurRadius(10); // 设置模糊半径 shadow_effect.setOffset(5, 5); // 设置阴影偏移量(x, y) shadow_effect.setColor(Qt::black); // 设置阴影颜色 } void paint(QPainter* painter, QRect rect) override { } }; template struct Wrapper : QMainWindow { Wrapper() { resize(400, 400); auto* w = new Widget(); auto* ww = static_cast(w); ww->setParent(this); ww->setFixedSize(200, 200); ww->move(100, 100); } }; class Wave_Widget : public QWidget { Q_OBJECT public: explicit Wave_Widget(QWidget* parent = nullptr) : QWidget(parent), m_offset(0) { setFixedSize(400, 300); // 设置窗口大小 // 定时器:每隔一段时间更新一次动画 m_timer = new QTimer(this); connect(m_timer, &QTimer::timeout, this, &Wave_Widget::update_wave); m_timer->start(16); // 约60fps // 初始化动画 m_animation = new QPropertyAnimation(this, "wavePosition"); m_animation->setDuration(1000); m_animation->setLoopCount(-1); // 无限循环 m_animation->setEasingCurve(QEasingCurve::Linear); } protected: void paintEvent(QPaintEvent* event) override { QPainter painter(this); painter.setBrush(Qt::blue); // 通过 m_offset 控制波浪的 y 坐标 int y = 150 + 50 * std::sin(m_offset); // 使用正弦波函数产生波浪效果 painter.drawEllipse(QPoint(200, y), 20, 20); // 绘制一个圆形 QWidget::paintEvent(event); } private slots: void update_wave() { // 每次更新时间,更新 m_offset 以形成波动效果 m_offset += 0.1; if (m_offset > 2 * (3.14159265358979323846)) { m_offset = 0; // 重新从0开始 } update(); // 更新界面 } private: float m_offset; // 波动的偏移量 QTimer* m_timer; QPropertyAnimation* m_animation; };