//Qt C++实现数字雨动画的源码
#include <qapplication>
#include <qwidget>
#include <qlabel>
#include <qtimer>
#include <qpainter>
class DigitalRain : public QWidget
{
public:
DigitalRain(QWidget *parent = nullptr)
: QWidget(parent)
{
setFixedSize(800, 600);
setWindowTitle("黑客帝国数字雨动画");
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &DigitalRain::updateAnimation);
timer->start(100);
}
protected:
void paintEvent(QPaintEvent *event) override
{
QPainter painter(this);
painter.setPen(Qt::green);
QFont font("Consolas", 18);
painter.setFont(font);
for (int i = 0; i < m_colCount; ++i) {
int size = m_items[i].size();
for (int j = 0; j < size; ++j) {
painter.drawText(m_items[i][j], i * m_itemSize, j * m_itemSize, m_itemSize, m_itemSize, Qt::AlignCenter, QString(QChar(m_items[i][j])));
}
}
}
private:
void updateAnimation()
{
for (int i = 0; i < m_colCount; ++i) {
int rowCount = m_items[i].size();
if (rowCount < m_rowCount) {
m_items[i].prepend(getRandomChar());
} else {
m_items[i].removeLast();
m_items[i].prepend(getRandomChar());
}
}
update();
}
QChar getRandomChar()
{
return QChar(0x30A0 + qrand() % (0x30FF - 0x30A0));
}
private:
int m_colCount = 80;
int m_rowCount = 50;
int m_itemSize = 12;
QVector<qvector<qchar>> m_items = QVector<qvector<qchar>>(m_colCount, QVector<qchar>(m_rowCount, getRandomChar()));
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DigitalRain w;
w.show();
return a.exec();
}
</qchar></qvector<qchar></qvector<qchar></qpainter></qtimer></qlabel></qwidget></qapplication>
Qt C++实现黑客帝国数字雨动画源码
文件列表
numberraintestFont.rar
(预估有个11文件)
numberraintest
numberrain.cpp
1KB
numberraintest.pro.user.a454262
77KB
mainwindow.h
417B
numberraintest.pro
630B
mainwindow.cpp
718B
numberraintest.pro.user.4fe5fc0
76KB
main.cpp
172B
mainwindow.ui
1KB
numberrain.h
4KB
暂无评论