qt更改背景颜色的动画

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H


#include <QWidget>
#include <QPainter>
#include <QPropertyAnimation>
#include <QPushButton>
#include <QLineEdit>


class Widget : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(int Gradient_Position READ Gradient_Position WRITE setGradient_Position)
    Q_PROPERTY(qreal Circle_Ratio READ Circle_Ratio WRITE setCircle_Ratio)
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

    QPropertyAnimation *animation;
    QPropertyAnimation* animation2;
    QPushButton *button;
    QLineEdit *lineEdit;

    void forward();
    void backward();

    int Gradient_Position() const;
    void setGradient_Position(const int&value);

    qreal Circle_Ratio() const;
    void setCircle_Ratio(const qreal&value);
protected:
    void paintEvent(QPaintEvent *event);
    void resizeEvent(QResizeEvent *event);

private:
    int m_Gradient_Position; 
    QColor Global_Color = QColor(255, 255, 255 , 255); 
    QString color_str;
    qreal m_Circle_Ratio = 0.0; 
    bool m_isForward = true; 
    
};
#endif // WIDGET_H
cpp 复制代码
#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent) {
    this->resize(1440, 880);

    animation = new QPropertyAnimation(this, "Gradient_Position");
    animation->setDuration(5000);
    animation->setStartValue(0);
    animation->setEndValue(360);
    animation->start();

    connect(animation, &QPropertyAnimation::finished, this, [this] {
        animation->start();
    });

    animation2 = new QPropertyAnimation(this, "Circle_Ratio");
    animation2->setDuration(1000);
    animation2->setStartValue(m_Circle_Ratio);
    animation2->setEndValue(1.0);

    button = new QPushButton(this);
    button->setText("更改背景颜色");
    button->setGeometry(20, 20, 100, 30);
    
    lineEdit = new QLineEdit(this);
    lineEdit->setGeometry(130, 20, 100, 30);

    connect(lineEdit, &QLineEdit::textChanged, this, [this](const QString &text) {
        color_str = text;
        update();
    });

    connect(button, &QPushButton::clicked, this, [this] {
        if (m_isForward) {
            this->forward();
            m_isForward = false;
        } else {
            this->backward();
            m_isForward = true;
        }
    });
}

Widget::~Widget() {}

void Widget::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    int w = this->width();
    int h = this->height();
    int sum = w + h;

    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing);
    painter.setPen(Qt::NoPen);
    painter.setBrush(QBrush(QColor(255, 255, 255, 255)));
    painter.drawRect(0, 0, w, h);
    
    painter.setBrush(QBrush(QColor(0, 0, 0, 255)));
    painter.drawEllipse(button->rect().bottomRight(), int(sum * m_Circle_Ratio), int(sum * m_Circle_Ratio));

    QConicalGradient gradient(w / 2, h, m_Gradient_Position);
    gradient.setColorAt(0, QColor(255, 167, 69, 255));
    gradient.setColorAt(0.125, QColor(254, 134, 159, 255));
    gradient.setColorAt(0.25, QColor(239, 122, 200, 255));
    gradient.setColorAt(0.375, QColor(160, 131, 237, 255));
    gradient.setColorAt(0.5, QColor(67, 174, 255, 255));
    gradient.setColorAt(0.625, QColor(160, 131, 237, 255));
    gradient.setColorAt(0.75, QColor(239, 122, 200, 255));
    gradient.setColorAt(0.875, QColor(254, 134, 159, 255));
    gradient.setColorAt(1, QColor(255, 167, 69, 255));
    painter.setBrush(gradient);

    QFont font;
    font.setPixelSize(h / 5);
    font.setFamily("微软雅黑");
    font.setBold(true);
    painter.setFont(font);
    painter.setPen(QPen(QBrush(gradient), 2));
    painter.drawText(QRect(0, 0, w, h), Qt::AlignCenter, color_str);
}

void Widget::resizeEvent(QResizeEvent *event) {}

void Widget::forward() {
    animation2->setDirection(QAbstractAnimation::Forward);
    animation2->start();
}

void Widget::backward() {
    animation2->setDirection(QAbstractAnimation::Backward);
    animation2->start();
}

int Widget::Gradient_Position() const {
    return m_Gradient_Position;
}

void Widget::setGradient_Position(const int &value) {
    if (m_Gradient_Position == value)
        return;

    m_Gradient_Position = value;
    update();
}

qreal Widget::Circle_Ratio() const {
    return m_Circle_Ratio;
}

void Widget::setCircle_Ratio(const qreal &value) {
    if (m_Circle_Ratio == value)
        return;

    m_Circle_Ratio = value;
    update();
}
cpp 复制代码
  Widget *w=new Widget();
    w->show();
相关推荐
云中飞鸿20 分钟前
linux中qt安装
开发语言·qt
少控科技33 分钟前
QT第6个程序 - 网页内容摘取
开发语言·qt
stevenson_aspdotnet2 小时前
QT5.15.12 编译备忘
qt
无小道4 小时前
QT——QFIie和QFileInfo文件类
开发语言·qt·命令模式
薛定谔的猫喵喵5 小时前
基于PyQt5的视频答题竞赛系统设计与实现
开发语言·qt·音视频
薛定谔的猫喵喵6 小时前
基于C++ Qt的唐代诗歌查询系统设计与实现
c++·qt·sqlite
枫叶丹47 小时前
【Qt开发】Qt界面优化(一)-> Qt样式表(QSS) 背景介绍
开发语言·前端·qt·系统架构
明月醉窗台18 小时前
qt使用笔记六之 Qt Creator、Qt Widgets、Qt Quick 详细解析
开发语言·笔记·qt
R_.L21 小时前
【QT】常用控件(按钮类控件、显示类控件、输入类控件、多元素控件、容器类控件、布局管理器)
开发语言·qt
无小道1 天前
Qt——常用控件
开发语言·qt