QT 等待框

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;
}

代码连接地址

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值