Qt 启动界面
作者:互联网
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QPaintEvent>
#include <QPainter>
#include <QMainWindow>
#include <QtMath>
#include <QList>
#include <QTimer>
#include <QFont>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void paintEvent(QPaintEvent *event);
private:
Ui::MainWindow *ui;
QTimer* timer = 0;
QTimer* timer2 = 0;
int indexOffse = 0;
QString showText = "";
QList<QColor> colorList;
QList<int>radiusList;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowFlags(Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground);
colorList.clear();
colorList.append(QColor(255,255,255));
colorList.append(QColor(245,253,255));
colorList.append(QColor(240,252,255));
colorList.append(QColor(222,243,249));
colorList.append(QColor(202,241,252));
colorList.append(QColor(189,236,249));
colorList.append(QColor(180,237,252));
colorList.append(QColor(161,231,255));
radiusList.clear();
int radius = 8;
for(int i =0; i < 8; i++)
{
radiusList.append(radius + 1*i);
}
showText = "加载中.. ";
timer = new QTimer(this);
timer2 = new QTimer(this);
connect(timer,&QTimer::timeout,[=]{
indexOffse--;
if(indexOffse <0)
indexOffse = 7;
update();
});
connect(timer2,&QTimer::timeout,[=]{
int len = showText.right(4).simplified().length();
if(len >=4)
len = 0;
showText = "加载中";
for(int i = 0; i < 4; i++)
{
if(i < len+1)
showText += ".";
else
showText += " ";
}
update();
});
timer->start(60);
timer2->start(200);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
int width = this->width();
int height = this->height();
painter.save();
painter.fillRect(this->rect(),QColor(0,0,0,2));
QList<QSize> sizeList;
qreal k = qSin( 45 * 3.1415926 / 180);
int centerWidth = 200;
int centerHeight = 200;
int centerX = (width) * 0.5;
int centerY = (height) * 0.5;
int rLen = 30;
sizeList.append(QSize(centerX-rLen, centerY));
sizeList.append(QSize(centerX-rLen*k, centerY- rLen*k));
sizeList.append(QSize(centerX , centerY - rLen));
sizeList.append(QSize(centerX+rLen*k , centerY-rLen*k ));
sizeList.append(QSize(centerX +rLen , centerY ));
sizeList.append(QSize(centerX +rLen*k, centerY +rLen*k));
sizeList.append(QSize(centerX , centerY +rLen));
sizeList.append(QSize(centerX -rLen*k, centerY+rLen*k ));
painter.setPen(QColor(0,0,0,100));
painter.setBrush(QColor(0,0,0,100));
painter.drawRoundedRect(QRect(centerX-centerWidth*0.5,centerY-centerHeight*0.5,centerWidth ,centerHeight), 20,20);
auto paintEllips = [=](QPainter& painter,QColor color, int radius, QSize size)
{
int x1 = size.width() - 0.5*radius;
int y1 = size.height() - 0.5*radius;
painter.setPen(color);
painter.setBrush(color);
painter.drawEllipse(x1,y1, radius,radius);
};
for(int i =0; i < sizeList.count(); i++)
{
int index =( i + indexOffse) % 8;
paintEllips(painter, colorList[index],radiusList[index],sizeList[i]);
}
painter.setPen(QColor(255,255,255));
painter.setBrush(QColor(255,255,255));
QTextOption textO;
textO.setAlignment(Qt::AlignCenter);
QFont font = painter.font();
font.setPointSize(16);
painter.setFont(font);
painter.drawText(QRectF(centerX-centerWidth*0.4,centerY+centerHeight*0.2,centerWidth*0.8 ,centerHeight*0.2),showText, textO);
painter.restore();
}
标签:QColor,界面,Qt,启动,int,rLen,painter,append,255 来源: https://blog.csdn.net/hss2799/article/details/113177591