Qt自定义进度条

作者:笑含半生 QQ:609162385
在这里插入图片描述

#ifndef KERNELPROGRESSWIDGET_H
#define KERNELPROGRESSWIDGET_H

#include <QWidget>
#include <QPaintEvent>
#include <QPainter>
#include <QTimer>


class KernelProgressWidget : public QWidget
{
    Q_OBJECT
public:
    explicit KernelProgressWidget(QWidget *parent = 0);

protected:
    void paintEvent(QPaintEvent *event);
    void drawBackgroundProgress(QPainter *painter);
    void drawValueProgress(QPainter *painter);
    void drawScaleLine(QPainter *painter);
signals:

public slots:
    void setValueColor(QColor color);
    void setBgColor(QColor color);
    void setStartColor(QColor color);
    void setEndColor(QColor color);

    void setLvalue(int value);
    void setRvalue(int value);
    void setTitle(QString value);
    void setCurrentValue(QString value);



    void acceptTimeout();
private:
    QColor valueColor;
    QColor bgColor;
    QColor startColor;
    QColor endColor;
    int lvalue;
    int rvalue;
    QTimer timer;
    QString title;
    QString currentValue;

};

#endif // KERNELPROGRESSWIDGET_H

#include "kernelprogresswidget.h"

KernelProgressWidget::KernelProgressWidget(QWidget *parent) : QWidget(parent)
{
    valueColor = QColor(100,190,130);
    bgColor = QColor(200,200,200);
    endColor = QColor(200,0,200);
    startColor = QColor(200,200,0);
    lvalue = 30;
    rvalue = 45;
    currentValue = "0.00";
    title = "Air";
    connect(&timer,SIGNAL(timeout()),this,SLOT(acceptTimeout()));
    timer.start(100);
}

void KernelProgressWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPen pen;
    pen.setColor("white");
    painter.setPen(pen);
    painter.drawText(QPointF(this->width()*0.35,this->height()*0.9),title);
    painter.drawText(QPointF(this->width()*0.35,this->height()*0.95),currentValue);

    drawScaleLine(&painter);
    painter.setRenderHint(QPainter::Antialiasing, true);
    drawBackgroundProgress(&painter);
    drawValueProgress(&painter);


}


void KernelProgressWidget::setValueColor(QColor color)
{
    valueColor = color;
}

void KernelProgressWidget::setBgColor(QColor color)
{
    bgColor = color;
}

void KernelProgressWidget::setStartColor(QColor color)
{
    startColor = color;
}

void KernelProgressWidget::setEndColor(QColor color)
{
    endColor = color;
}

void KernelProgressWidget::setLvalue(int value)
{
    if (this->lvalue != value) {
        this->lvalue = value;
        update();
    }
}

void KernelProgressWidget::setRvalue(int value)
{
    if (this->rvalue != value) {
        this->rvalue = value;
        update();
    }
}

void KernelProgressWidget::setTitle(QString value)
{
    if (this->title != value) {
        this->title = value;
        update();
    }
}

void KernelProgressWidget::setCurrentValue(QString value)
{
    if (this->currentValue != value) {
        this->currentValue = value;
        update();
    }
}

void KernelProgressWidget::acceptTimeout()
{
    lvalue+=5;
    rvalue+=5;
    if(lvalue >= 85 ){
        lvalue = 45;
    }
    if(rvalue >= 75 ){
        rvalue = 55;
    }
    update();
}

猜你喜欢

转载自blog.csdn.net/cqltbe131421/article/details/89081915