QT 等待框
可修改界面颜色并且可以调整显示位置
DlgProgressWait::DlgProgressWait(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
setFixedSize(350, 220);
setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
setStyleSheet("background-color: rgb(230, 255, 255)");
_nMaxValue = 10;
_bClockWise = true;
_bShowPercent = false;
_nCurrentValue = 0;
_nProgressNum = 1;
_nCurProgressMaxNum = 100;
_sDescribeInfo = "请稍等...";
_foreground = QColor(30, 100, 160);
_textColor = QColor(30, 100, 160);
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, [=]() {
_nCurrentValue += 1;
if (_nProgressNum < _nCurProgressMaxNum) {
_nProgressNum += 1;
}
repaint();
});
timer->start(50);
}
DlgProgressWait::~DlgProgressWait()
{
if (timer->isActive()) {
timer->stop();
delete timer;
timer = NULL;
}
}
void DlgProgressWait::paintEvent(QPaintEvent *)
{
int nSpace = 10;
int nDescribeInfoHeight = 50;
int nWidth = this->width();
int nHeight = this->height();
int nRoundCircleHeight = nHeight - (nDescribeInfoHeight + nSpace);
int side = qMin(nWidth, nRoundCircleHeight);
//绘制准备工作,启用反锯齿,平移坐标轴中心,等比例缩放
QPainter painter(this);
QFont font;
font.setPixelSize(28);
painter.setFont(font);
painter.setPen(_textColor);
QRect rect(0, nRoundCircleHeight + nSpace, nWidth, nDescribeInfoHeight);
painter.drawText(rect, Qt::AlignCenter, _sDescribeInfo);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
painter.translate(nWidth / 2, nRoundCircleHeight / 2 + nSpace);
painter.scale(side / 200.0, side / 200.0);
drawRoundCircle(&painter);
if (_bShowPercent) {
drawValue(&painter);
}
}
void DlgProgressWait::drawRoundCircle(QPainter *painter)
{
painter->save();
//painter->setPen(Qt::NoPen);
painter->setPen(_textColor);
int radius = 99;
int minRadius = radius / 6;
double angleStep = 360.0 / _nMaxValue;
double alpha = (double)1 / _nMaxValue;
if (!_bClockWise) {
angleStep = -angleStep;
}
//计算中心点坐标
double centerRadius = radius / 1.2;
for (int i = 0; i < _nMaxValue; i++) {
double angle = (_nCurrentValue + i) * angleStep;
double initX = centerRadius * qCos(degreesToRadians(angle));
double initY = centerRadius * qSin(degreesToRadians(angle));
int value = i * alpha * 255;
value = value < 30 ? 30 : value;
_foreground.setAlpha(value);
painter->setBrush(_foreground);
painter->drawEllipse(initX - minRadius, initY - minRadius, minRadius * 2, minRadius * 2);
}
painter->restore();
}
void DlgProgressWait::drawValue(QPainter *painter)
{
int radius = 50;
painter->save();
QString text = QString::number(_nProgressNum) + "%";
QFont font;
font.setPixelSize(35);
painter->setFont(font);
painter->setPen(_textColor);
QRect rect(-40, -25, 80, 50);
painter->drawText(rect, Qt::AlignCenter, text);
painter->restore();
}
double DlgProgressWait::degreesToRadians(double value) {
return value * M_PI / 180;
}
void DlgProgressWait::setCurProgressNum(int nCurProgressNum) {
_nCurProgressMaxNum = nCurProgressNum;
}
void DlgProgressWait::setClockWise(bool bClockWise) {
_bClockWise = bClockWise;
}
void DlgProgressWait::setShowPercent(bool bShowPercent) {
_bShowPercent = bShowPercent;
}
void DlgProgressWait::setForeground(QColor foreground) {
_foreground = foreground;
}
void DlgProgressWait::setTextColor(QColor textColor) {
_textColor = textColor;
}
void DlgProgressWait::setDescribeInfo(QString sDescribeInfo) {
_sDescribeInfo = sDescribeInfo;
}