找回密码
 立即注册
收起左侧

自定义仪表盘

2
回复
5718
查看
[复制链接]
累计签到:384 天
连续签到:1 天
来源: 2018-9-1 09:40:36 显示全部楼层 |阅读模式

马上注册,查看详细内容!注册请先查看:注册须知

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 huina_zhao 于 2018-9-4 16:10 编辑

自定义仪表盘类Gauge     Qt版本说明:Qt5.6.0

1.可设置表盘能显示的最大、最小值、当前值;
2.可设置数值精度;
3.可设置表盘大刻度个数、小刻度个数;
4.可设置表盘起始角度、终止角度;
5.可设置表盘颜色、刻度线颜色、刻度值颜色;
6.可设置指针颜色、文本颜色;
7.可设置指针类型。


    注:QPainter::scale()  //可实现缩放,不要太好用。

     int side = qMin(this->width(), this->height());
     QPainter painter(this);         painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);      
     painter.translate(this.rect().center());       
     painter.scale(side / 画布大小, side / 画布大小);


回复

使用道具 举报

累计签到:7 天
连续签到:1 天
2018-9-4 22:55:23 显示全部楼层
请问你是哪位?为何不备注原创作者?妹的,连控件名字都改掉了!起码备注下原创作者尊重下吧!

点评

sorry,大神息怒。 大神不是封装了好多控件在designer中么,下载了,属性栏有属性及默认值,码一下就出来了。 #ifndef PANEL_H #define PANEL_H #include class Gauge : public QWidget { Q_OBJECT Q_  详情 回复 发表于 2018-9-5 10:45
回复 支持 反对

使用道具 举报

累计签到:384 天
连续签到:1 天
2018-9-5 10:45:05 显示全部楼层
liudianwu 发表于 2018-9-4 22:55
请问你是哪位?为何不备注原创作者?妹的,连控件名字都改掉了!起码备注下原创作者尊重下吧! ...

sorry,大神息怒。


大神不是封装了好多控件在designer中么,下载了,属性栏有属性及默认值,码一下就出来了。

#ifndef PANEL_H
#define PANEL_H

#include <QWidget>

class Gauge : public QWidget
{
    Q_OBJECT

    Q_ENUMS(PointerStyle)

    Q_PROPERTY(double minValue READ getMinValue WRITE setMinValue)
    Q_PROPERTY(double maxValue READ getMaxValue WRITE setMaxValue)
    Q_PROPERTY(double value READ getValue WRITE setValue)
    Q_PROPERTY(int precision READ getPrecision WRITE setPrecision)
    Q_PROPERTY(int scaleMajor READ getScaleMajor WRITE setScaleMajor)
    Q_PROPERTY(int scaleMinor READ getScaleMinor WRITE setScaleMinor)
    Q_PROPERTY(int startAngle READ getStartAngle WRITE setStartAngle)
    Q_PROPERTY(int endAngle READ getEndAngle WRITE setEndAngle)
    Q_PROPERTY(bool animation READ getAnimation WRITE setAnimation)
    Q_PROPERTY(double animationStep READ getAnimationStep WRITE setAnimationStep)
    Q_PROPERTY(QColor arcColor READ getArcColor WRITE setArcColor)
    Q_PROPERTY(QColor scaleColor READ getScaleColor WRITE setScaleColor)
    Q_PROPERTY(QColor scaleNumColor READ getScaleNumColor WRITE setScaleNumColor)
    Q_PROPERTY(QColor pointerColor READ getPointerColor WRITE setPointerColor)
    Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor)
    Q_PROPERTY(PointerStyle pointerStyle READ getPointerStyle WRITE setPointerStyle)

public:
    enum PointerStyle{
        PointerStyle_Circle = 0,
        PointerStyle_Indicator = 1,
        //PointerStyle_IndicatorR = 2,
        PointerStyle_Triangle = 3,
    };

    explicit Gauge(QWidget *parent = 0);

protected:
    void paintEvent(QPaintEvent*);

private:
    void drawArc(QPainter *painter);
    void drawScale(QPainter *painter);
    void drawScaleNum(QPainter *painter);
    void drawText(QPainter *painter);
    void drawPointer(QPainter *painter);

private:
    double minValue;    //最大值
    double maxValue;    //最小值
    double value;       //当前值

    int precision;      //数值精度

    int scaleMajor;     //大刻度个数
    int scaleMinor;     //小刻度个数

    int startAngle;     //起始角度
    int endAngle;       //结束角度

    bool animation;     //是否有动画
    double animationStep;//动画间隔

    QColor arcColor;     //表盘颜色
    QColor scaleColor;   //刻度颜色
    QColor scaleNumColor;//刻度值颜色

    QColor pointerColor; //指针颜色
    QColor textColor;    //文本颜色

    PointerStyle pointerStyle; //指针类型

public:
    double getMinValue()       const;
    double getMaxValue()       const;
    double getValue()          const;

    int getPrecision()         const;

    int getScaleMajor()        const;
    int getScaleMinor()        const;

    int getStartAngle()        const;
    int getEndAngle()          const;

    bool getAnimation()        const;

    double getAnimationStep()  const;

    QColor getArcColor()       const;
    QColor getScaleColor()     const;
    QColor getScaleNumColor()  const;
    QColor getPointerColor()   const;
    QColor getTextColor()      const;

    PointerStyle getPointerStyle()  const;

public Q_SLOTS:
    void setMinValue(double minValue);
    void setMaxValue(double maxValue);
    void setValue(double value);

    void setPrecision(int precision);

    void setScaleMajor(int scaleMajor);
    void setScaleMinor(int scaleMinor);

    void setStartAngle(int startAngle);
    void setEndAngle(int endAngle);

    void setAnimation(bool animation);
    void setAnimationStep(double animationStep);

    void setArcColor(QColor arcColor);
    void setScaleColor(QColor scaleColor);
    void setScaleNumColor(QColor scaleNumColor);

    void setPointerColor(QColor pointerColor);
    void setTextColor(QColor textColor);
    void setPointerStyle(PointerStyle pointerStyle);
};

#endif // PANEL_H





#include "Gauge.h"

#include <QPainter>

Gauge::Gauge(QWidget *parent) : QWidget(parent)
{
    minValue = 0;
    maxValue = 100;
    value = 15;

    precision = 0;

    scaleMajor = 10;
    scaleMinor = 10;

    startAngle = 45;
    endAngle = 45;

    animation = false;
    animationStep = 0.5;

    arcColor = QColor(50,50,50,255);
    scaleColor = QColor(60,60,60,255);
    scaleNumColor = QColor(20,20,20,255);

    pointerColor = QColor(255,107,107,255);
    textColor = QColor(100,184,255,255);

    pointerStyle = PointerStyle_Indicator;//PointerStyle_Circle;//PointerStyle_Triangle;//PointerStyle_Indicator;
}

void Gauge::paintEvent(QPaintEvent *)
{
    int width = this->width();
    int height = this->height();

    int side = qMin(width,height);

    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing);
    painter.translate(this->rect().center());
    painter.scale(side/200.0,side/200.0);

    drawArc(&painter);
    drawScale(&painter);
    drawScaleNum(&painter);
    drawPointer(&painter);
    drawText(&painter);
}

void Gauge::drawArc(QPainter *painter)
{
    double startAngle = (360 - (90 - this->startAngle))  * 16;
    double spanAngle = (360 - this->startAngle - this->endAngle)  * 16;

    int outerRadius = 99;
    int innerRadius = 90;

    QRectF outerRect(-outerRadius, -outerRadius, outerRadius*2, outerRadius*2);
    QRectF innerRect(-innerRadius, -innerRadius, innerRadius*2, innerRadius*2);

    QPen pen = painter->pen();
    pen.setWidth(2);
    painter->setPen(pen);

    painter->save();
    pen.setColor(this->arcColor);
    painter->drawArc(outerRect, startAngle, spanAngle);
    painter->drawArc(innerRect,startAngle,spanAngle);
    painter->restore();
}

void Gauge::drawScale(QPainter *painter)
{
    int outerRadius = 99;
    int innerRadius = 90;

    int outerLineX = 85;

    double majorScaleAngel = (360 - this->startAngle - this->endAngle)/this->scaleMajor;
    double minorScaleAngel = majorScaleAngel/this->scaleMinor;

    painter->save();
    QPen pen = painter->pen();
    pen.setColor(this->scaleMajor);
    painter->rotate(90-this->endAngle);

    for(int i=0;i<=this->scaleMajor*this->scaleMinor;i++)
    {
        if(i!=0)
        {
            painter->rotate(-minorScaleAngel);
        }

        if(0 == i%this->scaleMajor)
        {
            painter->drawLine(QPointF(outerLineX,0),QPointF(outerRadius,0));
        }
        else
        {
            painter->drawLine(QPointF(innerRadius,0),QPointF(outerRadius,0));
        }
    }

    painter->restore();
}

void Gauge::drawScaleNum(QPainter *painter)
{
    double m_PI = 3.1415;

    double majorScaleAngel = (360 - this->startAngle - this->endAngle)*1.0/this->scaleMajor;
    double valueSpan = (this->maxValue - this->minValue)/this->scaleMajor;

    int textPix = 8;
    int textRadius = 75;

    QFont font = painter->font();
    font.setPointSize(textPix);
    painter->setFont(font);
    font.setFamily("Arial");


    QFontMetrics metrix(font);

    for(int i=0;i<=this->scaleMajor;i++)
    {
        double initAngle = (90-this->endAngle-i*majorScaleAngel)*m_PI/180.0;
        double scaleNumStartX = textRadius*cos(initAngle);
        double scaleNumStartY = textRadius*sin(initAngle);

        QString text = QString::number(this->maxValue-i*valueSpan,'f',this->precision);

        int scaleNumWidth = metrix.width(text);
        int scaleNumHeight = metrix.height();

        QRectF rect(scaleNumStartX - scaleNumWidth/2,scaleNumStartY - scaleNumHeight/2,scaleNumWidth,scaleNumHeight);
        painter->drawText(rect,text);
    }
}

void Gauge::drawText(QPainter *painter)
{
    int valuePix = 18;
    QFont font = painter->font();
    font.setPointSize(valuePix);
    font.setFamily("Arial");

    QString valueText = QString::number(this->value);

    QFontMetrics valueMetrix(font);
    int textWidth = valueMetrix.width(valueText);
    int textHeight = valueMetrix.height();
    QRect textRect(-textWidth/2,50,textWidth,textHeight);

    painter->setFont(font);

    QPen pen = painter->pen();
    pen.setColor(this->textColor);
    painter->setPen(pen);
    painter->drawText(textRect,valueText);
}

void Gauge::drawPointer(QPainter *painter)
{
    double totalAngel = 360 - this->startAngle - this->endAngle;
    double rotateAngel = totalAngel - totalAngel/(this->maxValue - this->minValue)*(this->value - this->minValue);
    int tailRadius = 20;
    int centerRadius = 26;

    QPainterPath path;
    path.setFillRule(Qt::WindingFill);

    if(pointerStyle == PointerStyle_Indicator)
    {
        int pointerRadius = 75;
        int centerRadius = 5;

        QPoint point1(0,-centerRadius);
        QPoint point2(pointerRadius,0);
        QPoint point3(0,centerRadius);

        QPolygon polygon;
        polygon<<point1<<point2<<point3;

        path.addPolygon(polygon);
    }
    else if(pointerStyle == PointerStyle_Triangle)
    {
        int triangleRadius = 70;
        int baseRadius = 60;
        int HeightRadius = 5;

        QPoint point1(baseRadius,-HeightRadius);
        QPoint point2(triangleRadius,0);
        QPoint point3(baseRadius,HeightRadius);

        QPolygon polygon;
        polygon<<point1<<point2<<point3;

        path.addPolygon(polygon);
    }
    else if(pointerStyle == PointerStyle_Circle)
    {
        int circlePosRadius = 80;
        int circleRadius = 12;

        path.addEllipse(QRect(circlePosRadius/2,-circleRadius/2,circleRadius,circleRadius));
    }

    path.addEllipse(QRect(-tailRadius/2,-tailRadius/2,tailRadius,tailRadius));


    QBrush brush = painter->brush();
    brush.setStyle(Qt::SolidPattern);
    brush.setColor(this->pointerColor.lighter(120));
    painter->setBrush(brush);

    QPainterPath centerPath;
    centerPath.addEllipse(QRect(-centerRadius/2,-centerRadius/2,centerRadius,centerRadius));
    painter->fillPath(centerPath,painter->brush());


    brush.setColor(this->pointerColor);
    painter->setBrush(brush);

    painter->save();
    painter->rotate(90-this->endAngle);
    painter->rotate(-rotateAngel);
    painter->fillPath(path,painter->brush());
    painter->restore();
}

double Gauge::getMinValue() const
{
    return minValue;
}

double Gauge::getMaxValue() const
{
    return maxValue;
}

double Gauge::getValue() const
{
    return value;
}

int Gauge::getPrecision() const
{
    return precision;
}

int Gauge::getScaleMajor() const
{
    return scaleMajor;
}

int Gauge::getScaleMinor() const
{
    return scaleMinor;
}

int Gauge::getStartAngle() const
{
    return startAngle;
}

int Gauge::getEndAngle() const
{
    return endAngle;
}

bool Gauge::getAnimation() const
{
    return animation;
}

double Gauge::getAnimationStep() const
{
    return animationStep;
}

QColor Gauge::getArcColor() const
{
    return arcColor;
}

QColor Gauge::getScaleColor() const
{
    return scaleColor;
}

QColor Gauge::getScaleNumColor() const
{
    return scaleNumColor;
}

QColor Gauge::getPointerColor() const
{
    return pointerColor;
}

QColor Gauge::getTextColor() const
{
    return textColor;
}

Gauge:ointerStyle Gauge::getPointerStyle() const
{
    return pointerStyle;
}

void Gauge::setMinValue(double minValue)
{
    this->minValue = minValue;
    update();
}

void Gauge::setMaxValue(double maxValue)
{
    this->maxValue = maxValue;
    update();
}

void Gauge::setValue(double value)
{
    this->value = value;
    update();
}

void Gauge::setPrecision(int precision)
{
    this->precision = precision;
    update();
}

void Gauge::setScaleMajor(int scaleMajor)
{
    this->scaleMajor = scaleMajor;
    update();
}

void Gauge::setScaleMinor(int scaleMinor)
{
    this->scaleMinor = scaleMinor;
    update();
}

void Gauge::setStartAngle(int startAngle)
{
    this->startAngle = startAngle;
    update();
}

void Gauge::setEndAngle(int endAngle)
{
    this->endAngle = endAngle;
    update();
}

void Gauge::setAnimation(bool animation)
{
    this->animation = animation;
    update();
}

void Gauge::setAnimationStep(double animationStep)
{
    this->animationStep = animationStep;
    update();
}

void Gauge::setArcColor(QColor arcColor)
{
    this->arcColor = arcColor;
    update();
}

void Gauge::setScaleColor(QColor scaleColor)
{
    this->scaleColor = scaleColor;
    update();
}

void Gauge::setScaleNumColor(QColor scaleNumColor)
{
    this->scaleNumColor = scaleNumColor;
    update();
}

void Gauge::setPointerColor(QColor pointerColor)
{
    this->pointerColor = pointerColor;
    update();
}

void Gauge::setTextColor(QColor textColor)
{
    this->textColor = textColor;
    update();
}

void Gauge::setPointerStyle(Gauge:ointerStyle pointerStyle)
{
    this->pointerStyle = pointerStyle;
    update();
}
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了