komany 发表于 2017-1-20 13:47:19

授人以鱼不如授人以渔2-QT圆形刻度盘或者进度条以及扇形和圆环各种计算公式

添加圆环。
这次应该比较全了,其他的背景图啥的贴个image等图片进去也好了,把这些就算都清清楚楚的写出来
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QLabel>
#include <QMainWindow>
#include <QPainter>
#include <qtimer.h>

namespace Ui {
class MainWindow;
}

typedef enum tagDrawType
{
   Line = 1,
   Circle = 2
}DrawType;

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void paintEvent(QPaintEvent *event);
    void DrawBkLine(QPainter* painter);
    void DrawBkCircle(QPainter* painter);
    void DrawBkCircle2(QPainter* painter);
    void gradientArc(QPainter *painter, int radius, double startAngle, int angleLength, int arcHeight, QRgb color);
public slots:
    void SetValue();

private:
    Ui::MainWindow *ui;
    QLabel    *l1;
    QLabel    *l2;
    QLabel    *l3;
    int u;
    QTimer   t;
};

#endif // MAINWINDOW_H

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <math.h>
#include <QPainter>
#include <QLibrary>
//当前值

//最大刻度
const int nMax = 100;
//最小刻度
const int nMin = 0;
//旋转角度
const double nPercentRotate = 60;
//
const int nMaxRadus = 150;

const int nMinRadus = 100;

const int nDistance = 0;
//最外面的大圆
const int nDistance2 = 0;
//原点
int nOrgXpos   = 0;
int nOrgYpos   = 0;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    u = 50;
    nOrgXpos = width() / 2;
    nOrgYpos = height() / 2;
    l1 = new QLabel(this);
    l1->setText(QString("%1%").arg(QString::number(u)));
    l1->move(nOrgXpos   ,nOrgYpos );
    l2 = new QLabel(this);
    l3 = new QLabel(this);
    //l2->setText(QString("Min0%"));
    //这个地方要注意
    //l2->move(nOrgXpos - nMinRadus, nOrgYpos + nMinRadus );
   // l3->setText(QString("Max100%"));
    //l3->move(nOrgXpos+ (nMinRadus ) , nOrgYpos +(nMinRadus ) );
    t.setInterval(100);
    connect(&t, SIGNAL(timeout()), this, SLOT(SetValue( )));
    t.start();
}

MainWindow::~MainWindow()
{
    t.stop();
    delete ui;
}

void MainWindow::SetValue()
{
    if(u == nMax) {u = 0; };
    u++;
    l1->setText(QString("%1%").arg(QString::number(u)));

}

/**
   * 参数二:半径
   * 参数三:开始的角度
   * 参数四:指扫取的角度 - 顺时针
   * 参数五:圆环的高度
   * 参数六:填充色
**/
void MainWindow::gradientArc(QPainter *painter,
                           int radius,
                              double startAngle,
                               int angleLength,
                              int arcHeight,
                                 QRgb color)
{
// 渐变色
    QRadialGradient gradient(0, 0, radius);
    gradient.setColorAt(0, Qt::white);
    gradient.setColorAt(1.0, color);
    painter->setBrush(gradient);
    QRectF rect(-radius, -radius, radius << 1, radius << 1);
    QPainterPath path;
    path.arcTo(rect, startAngle, angleLength);
    QPainterPath subPath;
    subPath.addEllipse(rect.adjusted(arcHeight, arcHeight, -arcHeight, -arcHeight));
    // path为扇形 subPath为椭圆
    path -= subPath;
    painter->setPen(Qt::darkBlue);
    painter->drawPath(path);
}

//圆环画法
void MainWindow::DrawBkCircle2(QPainter* painter)
{
    painter->save();
    painter->setRenderHint(QPainter::Antialiasing, true);
    // >> 1(右移1位)相当于width() / 2
    painter->translate(nOrgXpos + 2 * nMaxRadus, nOrgYpos );
    painter->setPen(QColor(122,122,77));
    int arcHeight = nMaxRadus - nMinRadus;
    double b = (360.0 / (nMax - nMin)); //根据实际最大最小值计算单位度数
    double a = u * b;
    gradientArc(painter, nMaxRadus,0, 360 , arcHeight, qRgb(211, 40, 100));
    gradientArc(painter, nMaxRadus,-0, -a, arcHeight, qRgb(22, 200, 10));
    painter->restore();;
    update();
}
//饼图
void MainWindow::DrawBkCircle(QPainter* painter)
{
    painter->save();
    painter->translate(nOrgXpos + 2 * nMaxRadus, nOrgYpos);
    painter->setRenderHint(QPainter::Antialiasing, true);
    //painter->translate(width() >> 1, height() >> 1);
    painter->setPen(QColor(122,122,77));
    int arcHeight = nMaxRadus - nMinRadus;
    double b = (360.0 / (nMax - nMin));
    double a = u * b;
    QRadialGradient gradient(0, 0, nMaxRadus);
    gradient.setColorAt(0, Qt::blue);
    gradient.setColorAt(1.0, QColor(55,222,122));
    painter->setBrush(gradient);
    QRectF rect_top(-nMaxRadus, -nMaxRadus, nMaxRadus << 1, nMaxRadus << 1);
    //乘以16是换算
    painter->drawPie(rect_top, 0 * 16, -a * 16);
    //painter.setPen(QColor(122,222,122));
    gradient.setColorAt(0, Qt::white);
    gradient.setColorAt(1.0, QColor(122,22,22));
    painter->setBrush(gradient);
    painter->drawPie(rect_top,-a * 16, -(360-a) * 16);
    painter->restore();
    update();
}
//刻度盘
void MainWindow::DrawBkLine(QPainter* p)
{
    QColor use(112,222,121);
    QColor nouse(223,123,111);
    double nRotate = (360.0 - 2 * nPercentRotate) / (nMax - nMin);
    p->setRenderHint(QPainter::Antialiasing);
    p->setRenderHint(QPainter::SmoothPixmapTransform);
    p->save();
    p->translate(nOrgXpos, nOrgYpos);
    p->rotate(nPercentRotate);
    p->setPen(use);

    for(int i = 0 ; i <= u; i++){
      p->drawLine(0,nMinRadus,0,nMaxRadus);
      p->rotate(nRotate);
    }
    p->setPen(nouse);
    for(int i = u + 1 ; i <= nMax ; i++){
      p->drawLine(0,nMinRadus,0,nMaxRadus);
      p->rotate(nRotate);
    }
    p->restore();
    p->save();
    QPen pen(QColor(111,222,33));
    p->setPen(pen);
    p->drawEllipse(nOrgXpos - nMinRadus + nDistance/2, nOrgYpos- nMinRadus+ nDistance/2 , nMinRadus * 2 - nDistance , nMinRadus * 2 - nDistance );

    p->restore();
    pen.setColor(QColor(255,5,6));
    //pen.setWidth(26);
    p->setPen(pen);
    p->drawEllipse(nOrgXpos -nMaxRadus - nDistance2/2, nOrgYpos-nMaxRadus -nDistance2/2 , nMaxRadus * 2 + nDistance2, nMaxRadus * 2 + nDistance2);
    p->restore();
    p->save();
    //表针的画法
    p->translate(nOrgXpos, nOrgYpos);
    pen.setColor(QColor(2,33,133));
   // pen.setWidth(0.1);
    p->setPen(pen);
    p->rotate(nPercentRotate);
    p->rotate(u * nRotate);
    p->drawLine(0, 0, 0, nMinRadus);
    p->restore();
    l1->setText(QString("%1%").arg(QString::number(u)));
    update();
}

void MainWindow::paintEvent(QPaintEvent *event)
{
   // setAttribute(Qt::WA_OpaquePaintEvent);
    QPixmap pixmap = QPixmap(size());
    pixmap.fill(this, 0, 0);
    QPainter painter(this);
    //painter.initFrom(this);
    DrawBkLine(&painter);
    DrawBkCircle(&painter);
    DrawBkCircle2(&painter);
    //painter.drawPixmap(0, 0, pixmap);
}




orgsss 发表于 2017-1-20 22:00:15

wow 楼主好强势啊

lwa1321 发表于 2017-1-21 09:10:36

大神级别,咯咯。
页: [1]
查看完整版本: 授人以鱼不如授人以渔2-QT圆形刻度盘或者进度条以及扇形和圆环各种计算公式