Qt实现数字雨
运行效果:
头文件:
#ifndef NUMBERRAIN_H
#define NUMBERRAIN_H
#include
#include
#include
#include
#include
#include
#include
#include
#define Qcout qDebug()
//字体大小和移动速度
#define FONT_SIZE 30
#define TIME_SPEED 80
//自定义数字雨类
class Block_Label
{
public:
qint64 Position = 0; //字符显示纵坐标变量
qint32 transparency = 0; //透明度变量
QWidget ParentWidget;
QVector<QLabel> NumberRainTextSingle; //字符数组
//字符移动排列函数
void NumberRainArrange()
{
//标签文本设置字体
QFont TempFont;
QPalette plt;
plt.setColor(QPalette::WindowText, Qt::green);
TempFont.setFamily(QStringLiteral("Vineta BT"));
//TempFont.setFamily(QStringLiteral("黑体"));
TempFont.setPointSize(FONT_SIZE);
//初始化标签并随机复制0、1
for(int i = 0; i < 40; i++)
{
//生成所有字符
//NumberRainTextSingle.append(new QLabel(QString::asprintf("%c", QRandomGenerator::global()->generate()%125), ParentWidget));
//生成0 1字符
NumberRainTextSingle.append(new QLabel(QString::asprintf("%d", QRandomGenerator::global()->generate()%2), ParentWidget));
}
//为所有标签设置字体颜色、大小
for(int i = 0; i < NumberRainTextSingle.size(); i++)
{
NumberRainTextSingle.at(i)->setFont(TempFont);
NumberRainTextSingle.at(i)->setPalette(plt);
NumberRainTextSingle.at(i)->setMinimumSize(FONT_SIZE + FONT_SIZE/5,FONT_SIZE + FONT_SIZE/5);
}
//创建随机纵坐标,移动标签,纵向排列
int Postiion_x = (QRandomGenerator::global()->generate() % ParentWidget->width());
for(int i = 0; i < NumberRainTextSingle.count(); i++)
{
NumberRainTextSingle.at(i)->move(Postiion_x, (FONT_SIZE + FONT_SIZE/5) * i);
}
//隐藏标签
for(int i = 0; i < NumberRainTextSingle.count(); i++)
{
NumberRainTextSingle.at(i)->hide();
}
}
//字符移动函数
void NumberRainMoveShow()
{
//透明度变量
transparency = 255;
this->NumberRainTextSingle.at(Position++)->show();
//为字符设置颜色,透明度
for(int i = Position; i >= 0; i -= 1)
{
QPalette plt;
QColor clr(21, 255, 21, transparency);
plt.setColor(QPalette::WindowText, clr);
this->NumberRainTextSingle.at(i)->setPalette(plt);
if(transparency > 0)
{
transparency -= 20; //透明度递减
}
}
//字符移出窗口判断
if(NumberRainTextSingle.at(Position)->pos().y() > ParentWidget->height()+200)
{
//释放内存
for(int i = 0; i < this->NumberRainTextSingle.count(); i++)
{
delete NumberRainTextSingle.at(i);
}
//清空数组并重新随机生成字符并排雷
this->NumberRainTextSingle.clear();
this->NumberRainArrange();
//在纵向0-20的位置随机生成字符出现纵坐标
Position = QRandomGenerator::global()->generate() % 20;
}
}
public:
Block_Label(QWidget *parent)
{
this->ParentWidget = parent;
NumberRainArrange();
}
~Block_Label()
{
//释放内存
for(int i = 0; i < this->NumberRainTextSingle.count(); i++)
{
delete NumberRainTextSingle.at(i);
}
}
};
class NumberRain : public QWidget
{
Q_OBJECT
public:
explicit NumberRain(QWidget *parent = nullptr);
~NumberRain();
private:
void StartUp(); //初始化函数
QTimer Timer_Speed; //字符移动速度定时器
QVector<Block_Label> NumberRainTextMulti;
public slots:
void NumberRainStart(); //启动槽函数
void NumberRainStop(); //停止槽函数
signals:
};
#endif // NUMBERRAIN_H
源文件:
#include “numberrain.h”
-
NumberRain::NumberRain(QWidget *parent)
-
QWidget{parent}
{
//启动初始化函数
this->StartUp();
//启动定时器
this->NumberRainStart();
}
NumberRain::~NumberRain()
{
//析构释放内存
delete Timer_Speed;
for(int i = 0; i < this->NumberRainTextMulti.count(); i++)
{
delete NumberRainTextMulti.at(i);
}
}
//【初始化函数】****************************************************
void NumberRain::StartUp()
{
//初始化60组数字雨
for(int i = 0; i < 60; i++)
{
this->NumberRainTextMulti.append(new Block_Label(this));
}
//初始化时间,链接信号槽、启动定时器
this->Timer_Speed = new QTimer(this);
connect(this->Timer_Speed, &QTimer::timeout, this, [=]
{
for(int i = 0; i < NumberRainTextMulti.count(); i++)
{
this->NumberRainTextMulti.at(i)->NumberRainMoveShow();
}
});
}
void NumberRain::NumberRainStart()
{
this->Timer_Speed->start(TIME_SPEED);
}
void NumberRain::NumberRainStop()
{
this->Timer_Speed->stop();
}
下载地址:
https://download.csdn.net/download/shaoxiloveyou/87448969