QT自定义控件之进度条

QT自定义控件之进度条_第1张图片

#ifndef QPROG_H
#define QPROG_H

#include 
#include 
#include 
#include 
#include 

class QProg : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(double value READ value WRITE setValue);
    Q_PROPERTY(double min READ minValue WRITE setMinValue);
    Q_PROPERTY(double max READ maxValue WRITE setMaxValue);
    Q_PROPERTY(int font READ font WRITE setFontDim);
    Q_PROPERTY(int numPrec READ numPrec WRITE setPrecision);
    Q_PROPERTY(QColor color READ color WRITE setBarColor);

    double value() const
    {
        return actVal;
    }
    double minValue() const
    {
        return minVal;
    }
    double maxValue() const
    {
        return maxVal;
    }
    int font() const
    {
        return fontDim;
    }
    int numPrec() const
    {
        return precision;
    }
    QColor color() const
    {
        return colBar;
    }
public:
    QProg(QWidget* parent = 0);
    QSize minimumSizeHint() const;
    QSize sizeHint() const;
    ~QProg();

signals:
    void valueChanged(double);

public slots:
    void setValue(double);
    void setMaxValue(double);
    void setMinValue(double);
    void setFontDim(int);
    void setPrecision(int);
    void setBarColor(QColor);

protected:
    void paintEvent(QPaintEvent *);
    void initValue();
    void paintBorder();
    void paintBar();
    void paintLine();
    void paintValue();

private:
    int fontDim;
    int precision;
    double lengthBar;
    double minVal;
    double maxVal;
    double actVal;
    QColor colBar;
};

#endif // QPROG_H
#include "qprog.h"

QProg::QProg(QWidget *parent)
    : QWidget(parent)
{
    initValue();
}

void QProg::paintEvent(QPaintEvent *)
{
    paintBorder();
    paintBar();
    paintLine();
    paintValue();
}

void QProg::initValue()
{
    minVal = 0;
    maxVal = 100;
    actVal = 0;
    fontDim = 20;
    precision = 0;
    lengthBar = 0;
    colBar = Qt::green;
}

void QProg::paintBorder()
{
    QPainter painter(this);
    painter.setWindow(0, 0, 470, 80);
    painter.setRenderHint(QPainter::Antialiasing);

    QLinearGradient linGrad(5, 40, 15, 40);
    linGrad.setColorAt(0, Qt::white);
    linGrad.setColorAt(1, Qt::black);
    linGrad.setSpread(QGradient::PadSpread);
    painter.setBrush(linGrad);
    QRectF border(5, 5, 460, 70);
    painter.drawRoundRect(border, 3);

    // value rect
    painter.setBrush(QColor(70, 70, 70));
    QRectF value(385, 10, 75, 60);
    painter.drawRoundRect(value, 15);
}

void QProg::paintBar()
{
    QPainter painter(this);
    painter.setWindow(0, 0, 470, 80);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setBrush(QColor(70, 70, 70));
    //background color
    QRectF back(20, 10, 360, 60);
    painter.drawRoundRect(back, 3);
    // chech value
    if (actVal > maxVal)
        return;
    if (actVal < minVal)
        return;
    // waiting state if min = max
    if(minVal == maxVal)
    {
        painter.setBrush(colBar);
        QRectF bar(40, 10, 40, 60);
        QRectF bar1(130, 10, 40, 60);
        QRectF bar2(220, 10, 40, 60);
        QRectF bar3(310, 10, 40, 60);
        painter.drawRoundRect(bar, 3);
        painter.drawRoundRect(bar1, 3);
        painter.drawRoundRect(bar2, 3);
        painter.drawRoundRect(bar3, 3);
        return;
    }
    // check positive or negative scale
    if (maxVal >= 0 && minVal >= 0 || maxVal >= 0 && minVal <= 0)
        lengthBar = 360-360 * (maxVal-actVal)/(maxVal-minVal);
    if (maxVal <= 0 && minVal <= 0)
        lengthBar = 360 * (minVal-actVal)/(minVal-maxVal);
    // length and color bar
    painter.setBrush(colBar);
    QRectF bar(20, 10, lengthBar, 60);
    painter.drawRoundRect(bar, 3);
    emit valueChanged(actVal);
}

void QProg::paintLine()
{
    QPainter painter(this);
    painter.setWindow(0, 0, 470, 80);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(QPen(Qt::black, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));

    for (int i = 0; i <= 40; i++)
    {
        painter.drawLine(20+(360/40*i), 10, 20+(360/40*i), 70);
    }
}

void QProg::paintValue()
{
    QPainter painter(this);
    painter.setWindow(0, 0, 470, 80);
    painter.setRenderHint(QPainter::Antialiasing);
    QRectF value(385, 10, 75, 60);
    QFont font("Arial", fontDim, QFont::Normal);
    painter.setFont(font);
    painter.setPen(QPen(Qt::white));
    double x = 100;
    double y = 360;
    QString val = QString("%1").arg(lengthBar*(x/y), 0, 'f', precision);
    val.append("%");
    painter.drawText(value, Qt::AlignCenter, val);
}

void QProg::setValue(double value)
{
    if (value > maxVal)
    {
        actVal = maxVal;
        update();
        return;
    }
    if (value < minVal)
    {
        actVal = minVal;
        update();
        return;
    }
    actVal = value;
    update();
}

void QProg::setMinValue(double min)
{
    if (min > maxVal)
    {
        minVal = maxVal;
        maxVal = min;
        actVal = minVal;
        update();
        return;
    }
    minVal = min;
    update();
}

void QProg::setMaxValue(double max)
{
    if (max < minVal)
    {
        maxVal = minVal;
        minVal = max;
        actVal = minVal;
        update();
        return;
    }
    maxVal = max;
    update();
}

void QProg::setFontDim(int font)
{
    fontDim = font;
    update();
}

void QProg::setPrecision(int numPrec)
{
    precision = numPrec;
    update();
}

void QProg::setBarColor(QColor color)
{
    colBar = color;
    update();
}


QSize QProg::minimumSizeHint() const
{
    return QSize(47, 8);
}

QSize QProg::sizeHint() const
{
    return QSize(470, 80);
}

QProg::~QProg()
{

}

 

你可能感兴趣的:(QT)