QT小控件-仪表盘
QT小控件-儀表盤
- 前言
- 一、程序代碼如下:
- wbgaugeone.pri:
- wbgaugeone.h:
- wbgaugeone.cpp:
- 總結(jié)
前言
具體使用說明見:https://blog.csdn.net/qq_39641107/article/details/121250077?spm=1001.2014.3001.5501
示意圖:
一、程序代碼如下:
wbgaugeone.pri:
HEADERS += wbgaugeone.h SOURCES += wbgaugeone.cppwbgaugeone.h:
#ifndef WBGAUGEONE_H #define WBGAUGEONE_H#include <QWidget> #include <math.h> #include <QPainter> #include <QtCore> #include <QFontMetrics> class WBgaugeone : public QWidget {Q_OBJECTpublic:WBgaugeone(QWidget *parent = 0);void setpointer_number(int pointer);//設(shè)置指針指向0-60void setcolorarc(QColor rarc); //最外層圓弧顏色void setcolorarc2(QColor rarc2); //中心圓漸變色起點(diǎn)顏色void setcolornumber(QColor number); //數(shù)字顏色void setcolorline(QColor line); //度量標(biāo)直線顏色void setcolorlips(QColor lips);//圓的終點(diǎn)顏色void setcolorpointer(QColor pointer);//指針顏色 private:QColor colorarc;//弧線顏色QColor colorarc2;//弧線顏色2QColor colornumber;//數(shù)字顏色QColor colorline;//直線顏色QColor colorelips;//圓顏色QColor colorpointer;//指針顏色QPen pen;//線QBrush brush;//刷子QRect rect;//視口int x, y;int pointer_number; //0-60 protected:void paintEvent(QPaintEvent *);void wbcircleEquation(int r, double j);void wbdrawArc(QPainter *painter);//畫弧線void wbdrawNumber(QPainter *painter, QString numberstr,int x, int y);//畫數(shù)字void wbdrawLine(QPainter *painter, int linewidth,int x1, int y1, int x2, int y2);//畫直線void wbdrawEllips(QPainter *painter);//畫圓void wbdrawPointer(QPainter *painter, int x1,int y1, int x2,int y2, int x3, int y3);//畫指針};#endif // WBGAUGEONE_Hwbgaugeone.cpp:
#include "wbgaugeone.h"WBgaugeone::WBgaugeone(QWidget *parent) :QWidget(parent) {colorarc = Qt::black;//弧線顏色colorarc2 = Qt::white;//弧線顏色2colornumber =Qt::red;//數(shù)字顏色colorline = Qt::black;//直線顏色colorelips = Qt::blue;//圓顏色colorpointer = Qt::blue;//指針顏色pointer_number = 0;this->repaint(); } void WBgaugeone::wbcircleEquation(int r ,double j)//根據(jù)x坐標(biāo)獲得圓的y坐標(biāo),sign判斷y的正負(fù)性 {double val = 3.14159265 / 180;if (j>180){x = -int(r*fabs(cos(val*j)));y = int(r*fabs(sin(val*j)));}else if (j<=180 && j>90){x = -int(r*fabs(cos(val*j)));y = -int(r*fabs(sin(val*j)));}else if (j<=90 && j>0){x = int(r*fabs(cos(val*j)));y = -int(r*fabs(sin(val*j)));}else{x = int(r*fabs(cos(val*j)));y = int(r*fabs(sin(val*j)));} } void WBgaugeone::wbdrawArc(QPainter *painter)//畫弧線 {pen = QPen();pen.setWidth(25);//25pen.setColor(colorarc);pen.setStyle(Qt::SolidLine);pen.setCapStyle(Qt::RoundCap);pen.setJoinStyle(Qt::BevelJoin);painter->setPen(pen);painter->save();painter->translate(250,250);rect.setRect(-235,-235,470,470);painter->drawArc(rect,-45*16,270*16);painter->restore();} void WBgaugeone::wbdrawNumber(QPainter *painter, QString numberstr,int x, int y)//畫數(shù)字 {QFont font;font.setFamily("微軟雅黑");//字體 // font.setPixelSize(25);//文字像素大小font.setPointSize(25);//文字大小 // QFontMetrics textsize = QFontMetrics(font); // QRect textrect = QRect(textsize.boundingRect(numberstr));painter->setFont(font);int widthofTitle = painter->fontMetrics().width(numberstr);//字符串顯示的像素大小int heightofTitle = painter->fontMetrics().height();pen = QPen();pen.setWidth(3);//3pen.setColor(colornumber);pen.setStyle(Qt::SolidLine);pen.setCapStyle(Qt::FlatCap);pen.setJoinStyle(Qt::BevelJoin);painter->setPen(pen);painter->save();painter->translate(250,250);painter->drawText(x-widthofTitle/2,y+heightofTitle/4,numberstr);painter->restore(); } void WBgaugeone::wbdrawLine(QPainter *painter, int linewidth,int x1, int y1,int x2, int y2)//畫直線 {pen = QPen();pen.setWidth(linewidth);pen.setColor(colorline);pen.setStyle(Qt::SolidLine);pen.setCapStyle(Qt::RoundCap);pen.setJoinStyle(Qt::BevelJoin);painter->setPen(pen);painter->save();painter->translate(250,250);painter->drawLine(x1,y1,x2,y2);painter->restore();} void WBgaugeone::wbdrawEllips(QPainter *painter)//畫圓 {pen = QPen();pen.setWidth(2);//25pen.setColor(colorelips);pen.setStyle(Qt::SolidLine);pen.setCapStyle(Qt::RoundCap);pen.setJoinStyle(Qt::BevelJoin);painter->setPen(pen); // brush = QBrush(); // brush.setColor(colorelips); // brush.setStyle(Qt::SolidPattern); // painter->setBrush(brush);rect.setRect(-25,-25,50,50);QRadialGradient radial = QRadialGradient(0,0,25,0,0);radial.setColorAt(0,colorarc2);radial.setColorAt(1,colorelips);painter->save();painter->translate(250,250);painter->setBrush(radial);painter->drawEllipse(rect);painter->restore(); } void WBgaugeone::wbdrawPointer(QPainter *painter,int x1,int y1, int x2,int y2, int x3, int y3)//畫指針 {pen = QPen();pen.setWidth(1);//25pen.setColor(colorpointer);pen.setStyle(Qt::SolidLine);pen.setCapStyle(Qt::RoundCap);pen.setJoinStyle(Qt::BevelJoin);painter->setPen(pen);brush = QBrush();brush.setColor(colorpointer);brush.setStyle(Qt::SolidPattern);painter->setBrush(brush);painter->save();painter->translate(250,250);painter->drawPolygon(QPolygon({QPoint(x1,y1),QPoint(x2,y2),QPoint(x3,y3)}));painter->restore(); } void WBgaugeone::paintEvent(QPaintEvent *)//繪制 {QPainter wbpainter(this);wbpainter.setRenderHint(QPainter::Antialiasing);wbpainter.setRenderHint(QPainter::TextAntialiasing);rect = QRect(0,0,this->width(),this->height());wbpainter.setViewport(rect);wbpainter.setWindow(0,0,500,500);this->wbdrawArc(&wbpainter);double j=225;for (int i=0;i<=12;i++){QString istr = QString::number(i,10);this->wbcircleEquation(190,j);this->wbdrawNumber(&wbpainter,istr,x,y);for(int line_i = 0;line_i<=4;line_i++){int x1,x2,y1,y2,r;int linewidth;if (line_i == 0){linewidth = 5;r=110;}else{linewidth = 2;r=130;}this->wbcircleEquation(150,j-line_i*4.5);x1=x;y1=y;this->wbcircleEquation(r,j-line_i*4.5);x2=x;y2=y;this->wbdrawLine(&wbpainter,linewidth,x1,y1,x2,y2);if (j == -45 && line_i == 0)break;}j = j-22.5;}j=225;int x1,x2,x3,y1,y2,y3;this->wbcircleEquation(140,j-pointer_number*4.5);x1=x;y1=y;this->wbcircleEquation(25,j-pointer_number*4.5-22.5);x2=x;y2=y;this->wbcircleEquation(25,j-pointer_number*4.5+22.5);x3=x;y3=y;this->wbdrawPointer(&wbpainter,x1,y1,x2,y2,x3,y3);this->wbdrawEllips(&wbpainter); }//接口函數(shù) void WBgaugeone::setpointer_number(int pointer)//設(shè)置指針指向0-60 {if(pointer>=0&&pointer<=60){pointer_number = pointer;this->repaint();} } void WBgaugeone::setcolorarc(QColor rarc) //最外層圓弧顏色 {colorarc = rarc;this->repaint(); } void WBgaugeone::setcolorarc2(QColor rarc2) //中心圓漸變色起點(diǎn)顏色 {colorarc2 = rarc2;this->repaint(); } void WBgaugeone::setcolornumber(QColor number) //數(shù)字顏色 {colornumber = number;this->repaint(); } void WBgaugeone::setcolorline(QColor line) //度量標(biāo)直線顏色 {colorline = line;this->repaint(); } void WBgaugeone::setcolorlips(QColor lips)//圓的終點(diǎn)顏色 {colorelips = lips;this->repaint(); } void WBgaugeone::setcolorpointer(QColor pointer)//指針顏色 {colorpointer = pointer;this->repaint(); }總結(jié)
儀表盤
制作人:博咯
僅限他人學(xué)習(xí),交流使用
void setpointer_number(int pointer);//設(shè)置指針指向0-60
void setcolorarc(QColor rarc); //最外層圓弧顏色
void setcolorarc2(QColor rarc2); //中心圓漸變色起點(diǎn)顏色
void setcolornumber(QColor number); //數(shù)字顏色
void setcolorline(QColor line); //度量標(biāo)直線顏色
void setcolorlips(QColor lips);//圓的終點(diǎn)顏色
void setcolorpointer(QColor pointer);//指針顏色
該儀表盤主要通過QPainter進(jìn)行繪制,可能有沉余代碼,可以更加簡潔,謝謝大家點(diǎn)贊及指導(dǎo)!
總結(jié)
- 上一篇: Python数据可视化:2018年北上广
- 下一篇: C++ 打怪小游戏3.1