Qt自定义控件--等待进度条

Qt自定义控件–等待进度条

1.可以设置背景颜色、放射线条颜色、文本颜色
2.可以设置进度显示从0-100%
3.进度条可以动态显示
在这里插入图片描述

头文件

#ifndef PROGRESS_H
#define PROGRESS_H

#include <QWidget>
#include <QTimer>
#include <QtMath>
#include <QPainter>

class Progress : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(QColor BgColor READ getBgColor WRITE setBgColor)
    Q_PROPERTY(QColor TextColor READ getTextColor WRITE setTextColor)
    Q_PROPERTY(QColor BarColor READ getBarColor WRITE setBarColor)

public:
    explicit Progress(QWidget *parent = nullptr);
    QColor getBgColor()               const;    //获取背景颜色
    QColor getTextColor()             const;    //获取文本颜色
    QColor getBarColor()              const;    //获取进度条颜色

private:
    bool ProgressRun;   //进度条运行状态
    QColor BgColor;     //背景颜色
    QColor TextColor;   //文本颜色
    QColor BarColor;    //进度条颜色
    QVector<QColor> rColor; //进度条颜色区分数组
    int ProgressNum;    //进度
    QTimer *timerrun;   //定时器
    int startAngle;     //进度条开始角度
protected:
    void paintEvent(QPaintEvent *);
    void drawBar(QPainter *painter);    //绘制进度条
    void drawBg(QPainter *painter);     //绘制背景
    void drawText(QPainter *painter);   //绘制文字

signals:

public slots:
    void setBgColor(const QColor &BgColor);         //设置背景颜色
    void setTextColor(const QColor &TextColor);     //设置文本颜色
    void setBarColor(const QColor &BarColor);       //设置进度条颜色
    void setProgressNum(const int &ProgressNum);    //设置进度
    void prorun();		
    void startRun();	//开始动态显示
    void stopRun();		//停止动态显示

};

#endif // PROGRESS_H

CPP

#include "progress.h"

Progress::Progress(QWidget *parent) : QWidget(parent)
{
    BgColor = QColor(255,255,255,0);
    BarColor = QColor(Qt::blue);
    TextColor = QColor(Qt::black);
    BarColor.setAlpha(50);
    rColor.resize(5);
    for (int i = 0; i < 5; i++) {
        rColor[i] = QColor(BarColor);
        rColor[i].setAlpha(100 + i * 20);
    }
    startAngle = 45;
    ProgressNum = 0;
    ProgressRun = false;
    timerrun = new QTimer(this);
    connect(timerrun, SIGNAL(timeout()), this, SLOT(prorun()));
    timerrun->setInterval(100);

}


void Progress::paintEvent(QPaintEvent *)
{
    int width = this->width();
    int height = this->height();
    int side = qMin(width, height);
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    painter.translate(width / 2, height / 2);
    painter.scale(side / 200.0, side / 200.0);

    drawBg(&painter);
    drawBar(&painter);
    drawText(&painter);
}


void Progress::drawBg(QPainter *painter)
{
    int radius = 100;
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(BgColor);
    painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
    painter->restore();
}

void Progress::drawBar(QPainter *painter)
{
    int radius = 95;
    painter->save();
    QPen pen;
    pen.setCapStyle(Qt::RoundCap);
    pen.setWidthF(10.0);
    painter->setBrush(Qt::NoBrush);
    if (ProgressRun == false) {
        for (int i = 0; i < 8; i++) {
            pen.setColor(BarColor);
            painter->setPen(pen);
            float x = qCos(double(startAngle + 45 * i) * M_PI / 180) * radius;
            float y = qSin(double(startAngle + 45 * i) * M_PI / 180) * radius;
            float x1 = qCos(double(startAngle + 45 * i) * M_PI / 180) * 50;
            float y1 = qSin(double(startAngle + 45 * i) * M_PI / 180) * 50;
            QLineF line(x1, y1, x, y);
            painter->drawLine(line);
        }
    }
    else {
        for (int i = 0; i < 5; i++) {
            pen.setColor(rColor[i]);
            painter->setPen(pen);
            float x = qCos(double(startAngle + 45 * i) * M_PI / 180) * radius;
            float y = qSin(double(startAngle + 45 * i) * M_PI / 180) * radius;
            float x1 = qCos(double(startAngle + 45 * i) * M_PI / 180) * 60;
            float y1 = qSin(double(startAngle + 45 * i) * M_PI / 180) * 60;
            QLineF line(x1, y1, x, y);
            painter->drawLine(line);
        }
        for (int i = 0; i < 3; i++) {
            pen.setColor(BarColor);
            painter->setPen(pen);
            float x = qCos(double(startAngle + 45 * (i + 5)) * M_PI / 180) * radius;
            float y = qSin(double(startAngle + 45 * (i + 5)) * M_PI / 180) * radius;
            float x1 = qCos(double(startAngle + 45 * (i + 5)) * M_PI / 180) * 50;
            float y1 = qSin(double(startAngle + 45 * (i + 5)) * M_PI / 180) * 50;
            QLineF line(x1, y1, x, y);
            painter->drawLine(line);
        }
    }
    painter->restore();
}

void Progress::drawText(QPainter *painter)
{
    int radius = 50;
    painter->save();
    QString text = QString::number(ProgressNum) + "%";
    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();
}

void Progress::prorun()
{
    startAngle += 45;
    this->update();
}

QColor Progress::getBarColor() const
{
    return this->BarColor;
}

QColor Progress::getBgColor() const
{
    return this->BgColor;
}

QColor Progress::getTextColor() const
{
    return this->TextColor;
}

void Progress::setBarColor(const QColor &BarColor)
{
    if (this->BarColor != BarColor) {
        this->BarColor = BarColor;
        for (int i = 0; i < 5; i++) {
            rColor[i] = QColor(BarColor);
            rColor[i].setAlpha(100 + i * 20);
        }
        this->update();
    }
}

void Progress::setBgColor(const QColor &BgColor)
{
    if (this->BgColor != BgColor) {
        this->BgColor = BgColor;
        this->update();
    }
}

void Progress::setTextColor(const QColor &TextColor)
{
    if (this->TextColor != TextColor) {
        this->TextColor = TextColor;
        this->update();
    }
}

void Progress::setProgressNum(const int &ProgressNum)
{
    if (this->ProgressNum != ProgressNum) {
        this->ProgressNum = ProgressNum;
        this->update();
    }
}

void Progress::startRun()
{
    if (!timerrun->isActive()) {
        timerrun->start();
        ProgressRun = true;
        this->update();
    }
}

void Progress::stopRun()
{
    if (timerrun->isActive()) {
        timerrun->stop();
        ProgressRun = false;
        this->update();
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值