近期由于工作不忙,就来做做自定义控件,做的不好,请给建议,以后会改。
那就直接本入正题吧:
头文件:
#ifndefMAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
protected:
virtual void paintEvent(QPaintEvent *event);
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected slots:
void move(int);
private:
Ui::MainWindow *ui;
int value;
private:
//绘画初始化
void painter_init(QPainter&);
//绘制圆环1
void draw_Ellipse(QVector<double>&,QVector<double>&,QPainter &);
//重载draw_Ellipse()函数,用于绘制圆环2
void draw_Ellipse(QVector<double>&,QVector<double>&,QPainter &,QRadialGradient &);
//绘画扇形
void draw_pie(QPainter &);
//绘制文本和刻度
void draw_text_line(QPainter&);
//绘制指针
void draw_point(QPainter&);
};
#endif // MAINWINDOW_H源文件:
#include"mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
value=0;
ui->horizontalSlider->setRange(0,100);
QObject::connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(move(int)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter paint(this);
painter_init(paint);
//////////////[1]获取圆环
QVector<double> num={200,200,300,300};
QVector<double> num1={220,220,260,260};
draw_Ellipse(num, num1,paint);
////////////[2]获取圆环
QVector<double> num2={220,220,260,260};
QVector<double> num3={260,260,180,180};
QRadialGradient radgrient(220,220,90,260,260);
draw_Ellipse(num2,num3,paint,radgrient);
////////////////////////[3]获取扇形,并重叠。
draw_pie(paint);
paint.setBrush(QBrush(Qt::darkRed,Qt::SolidPattern));
QVector<double> num4={320,320,60,60};
QVector<double> num5={327.5,327.5,45,45};
draw_Ellipse(num4,num5,paint);
paint.setBrush(QBrush(Qt::gray,Qt::SolidPattern));
QPainterPath big4;
big4.addEllipse(327.5,327.5,45,45);
//////////////[4]显示刻度以及文本
draw_text_line(paint);
//////////////【5】////////////////////显示指针
draw_point(paint);
}
void MainWindow::move(int num)
{
value=num;
qDebug()<<value;
update();
}
void MainWindow::painter_init(QPainter &paint)
{
paint.setRenderHint(QPainter::Antialiasing,true);
paint.setBrush(QBrush(Qt::darkBlue,Qt::SolidPattern));
}
void MainWindow::draw_Ellipse(QVector<double> &num, QVector<double> &num1,QPainter &paint)
{
QPainterPath big;
big.addEllipse(num.at(0),num.at(1),num.at(2),num.at(3));
QPainterPath small;
big.addEllipse(num1.at(0),num1.at(1),num1.at(2),num1.at(3));
QPainterPath path;
path=big-small;
paint.drawPath(path);
}
void MainWindow::draw_Ellipse(QVector<double> &num, QVector<double> &num1, QPainter &paint, QRadialGradient &radgrient)
{
radgrient.setColorAt(0.0,Qt::darkGray);
radgrient.setColorAt(0.2,Qt::white);
radgrient.setColorAt(0.5,Qt::darkGray);
paint.setBrush(QBrush(Qt::gray,Qt::SolidPattern));
paint.setPen(Qt::NoPen);
draw_Ellipse(num,num1,paint);
}
void MainWindow::draw_pie(QPainter &paint)
{
paint.setBrush(QBrush(Qt::darkGreen,Qt::SolidPattern));
// 扇形起始角度
int startAngle = -40*16 ;
// 扇形覆盖范围(120度的)
int spanAngle = 260*16;
QRectF rect_top(260,260, 180.0, 180.0);
QRectF rect_top1(280,280, 140.0, 140.0);
QRectF rect_top2(260,260, 180.0, 180.0);
paint.drawPie(rect_top, startAngle, spanAngle);
paint.setBrush(QBrush(Qt::gray,Qt::SolidPattern));
paint.drawPie(rect_top1, startAngle, spanAngle);
paint.drawPie(rect_top2, 220*16, 100*16);
}
void MainWindow::draw_text_line(QPainter &paint)
{
paint.translate(350,350);
paint.setPen(Qt::black);
for(int i=0;i<=25;i++)
{
if(i%5==0)
{
paint.drawLine(0,-90,0,-100);
switch(i/5)
{
case 0:paint.drawText(-5,-103, tr("%1").arg(50));break;
case 1:paint.drawText(-5,-103, tr("%1").arg(60));break;
case 2:paint.drawText(-5,-103, tr("%1").arg(70));break;
case 3:paint.drawText(-5,-103, tr("%1").arg(80));break;
case 4:paint.drawText(-5,-103, tr("%1").arg(90));break;
case 5:paint.drawText(-5,-103, tr("%1").arg(100));break;
}
}
else {
paint.drawLine(0,-95,0,-100);
}
paint.rotate(5.2);
}
paint.rotate(100-5.2);
for(int i=0;i<=25;i++)
{
if(i%5==0)
{
paint.drawLine(0,-90,0,-100);
switch(i/5)
{
case 0:paint.drawText(-5,-103, tr("%1").arg(0));break;
case 1:paint.drawText(-5,-103, tr("%1").arg(10));break;
case 2:paint.drawText(-5,-103, tr("%1").arg(20));break;
case 3:paint.drawText(-5,-103, tr("%1").arg(30));break;
case 4:paint.drawText(-5,-103, tr("%1").arg(40));break;
}
}
else {
paint.drawLine(0,-95,0,-100);
}
paint.rotate(5.2);
}
}
void MainWindow::draw_point(QPainter &paint)
{
QPoint hand[3]={
QPoint(0,-90),
QPoint(4,-25),
QPoint(-4,-25),
};
QColor handcolor(200,10,10,200);
paint.setBrush(handcolor);
paint.save();
paint.restore();
paint.drawText(-6,3,tr("%1").arg(value));
paint.rotate(220+5.2);
paint.save();
paint.rotate(value*2.6);
paint.drawConvexPolygon(hand,3);
paint.restore();
}