QFont font("Arial Rounded MT Bold");
// 指针形状 - 多边形
/*static const QPoint secondHand[4] = {
QPoint(7, -12),//Y值控制指针在圆环的位置
QPoint(-7, -12),
QPoint(-1, -95),
QPoint(1, -95)
};*/
// 指针及其他位置颜色
//QColor backgroundColor(94, 94, 94, 255);//圆盘背景颜色
QColor redHandleColor(255, 0, 0);//指针颜色
//QColor secondColor(0, 0, 0, 255);//10步长的刻度颜色 黑
//QColor minuteColor(255, 255, 255, 255);//小格的刻度颜色
QColor halfColor(255, 255, 255, 255);//半格的刻度颜色 白
//QColor maxColor(255,0,0,255); // 红色
//QColor minColor(0,255,127,255); //绿色
QColor formColor(0,0,223,255); //蓝色
int side = qMin(width(), height());
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); //反锯齿
// 平移坐标系原点至中心点
painter.translate(width() / 2, height() / 2);
// 缩放 //控制刻度的半径
painter.scale(side / 300.0, side / 300.0);
//绘制背景黑色圆盘 backgroundColor
//QPen bgpen;
//bgpen.setColor(backgroundColor);
//bgpen.setWidth(120);
//painter.setPen(bgpen);
// 改了一下,绘制圆弧 //线
//painter.setPen(QPen(Qt::black,2,Qt::SolidLine));
//painter.drawArc(-98,-98,195,195,0,5760);
// 绘制指针
/*painter.setPen(Qt::NoPen);
painter.setBrush(redHandleColor);
painter.save();
//一共270度,分150格,每一格1.8度
painter.rotate(225+index*4.5);//180+(10*4.5) // 可以控制指针指向任意数值
painter.drawConvexPolygon(secondHand, 4);
painter.restore();
//中心画一个红色圆环
QPen pen;
pen.setColor(QColor(255,0,0));
pen.setWidth(10);
painter.setPen(pen);
painter.drawArc(-10,-10,20,20,0,5760);*/
/*painter.setPen(halfColor);
// 绘制刻度线 (270度 / 15 = 18度)每大格间距18度
for (int i = 0; i < 8; ++i){
if(i==2){
}else{
painter.drawLine(88, 0, 96, 0);
}
painter.rotate(45);//按给定角度进行顺时针旋转
}
// 创建渐变
QConicalGradient gradient(0, 0, 270); // 从正上方开始逆时针渐变
gradient.setColorAt(0, Qt::red); // 设置渐变起始颜色为红色
gradient.setColorAt(0.75, QColor(0, 255, 0, 127)); // 在135度处设置浅绿色
gradient.setColorAt(1, Qt::green); // 设置渐变结束颜色为绿色
// 设置画笔
QPen penn;
penn.setBrush(gradient); // 使用渐变作为画刷
penn.setWidth(10); // 设置画笔宽度为5
penn.setCapStyle(Qt::RoundCap); // 设置画笔为圆刷
// 绘制圆环
painter.setPen(penn);
painter.drawArc(QRectF(-100, -100, 200, 200), -45 * 16, 270 * 16); // 绘制渐变圆环*/
int a=0,b=0;
painter.setPen(formColor);
// 绘制内刻度线 每格间隔1.8度
for (int j = 0; j < 80; ++j) { // 此80为所有小格数 下面减去20 = 6
if(j>10 && j<30){
}else{
painter.drawLine(90, 0, 96, 0);
if(j<=10){
if(j*4.5==(135+index*4.5-360)){
if((index-1)>0){painter.rotate(-4.5);painter.drawLine(86, 0, 96, 0);a=1;}
if((index-2)>0){painter.rotate(-4.5);painter.drawLine(87, 0, 96, 0);b=1;}
if(a){painter.rotate(4.5);a=0;}if(b){painter.rotate(4.5);b=0;}
painter.drawLine(85, 0, 96, 0);
if((index+1)<59){painter.rotate(4.5);painter.drawLine(86, 0, 96, 0);a=1;}
if((index+2)<59){painter.rotate(4.5);painter.drawLine(87, 0, 96, 0);b=1;}
if(a){painter.rotate(-4.5);a=0;}if(b){painter.rotate(-4.5);b=0;}
}
}else{
if(j*4.5==(135+index*4.5)){
if((index-1)>0){painter.rotate(-4.5);painter.drawLine(86, 0, 96, 0);a=1;}
if((index-2)>0){painter.rotate(-4.5);painter.drawLine(87, 0, 96, 0);b=1;}
if(a){painter.rotate(4.5);a=0;}if(b){painter.rotate(4.5);b=0;}
painter.drawLine(85, 0, 96, 0);
if((index+1)<59){painter.rotate(4.5);painter.drawLine(86, 0, 96, 0);a=1;}
if((index+2)<59){painter.rotate(4.5);painter.drawLine(87, 0, 96, 0);b=1;}
if(a){painter.rotate(-4.5);a=0;}if(b){painter.rotate(-4.5);b=0;}
}
}
}
painter.rotate(4.5);
}
// 创建渐变
QConicalGradient gradient(0, 0, 270); // 从正上方开始逆时针渐变
gradient.setColorAt(1, QColor(0, 65, 67, 79));
double radius1 = 105;
// 设置画笔
QPen penn;
penn.setBrush(gradient); // 使用渐变作为画刷
penn.setWidth(3); // 设置画笔宽度为5
penn.setCapStyle(Qt::RoundCap); // 设置画笔为圆刷
painter.setPen(penn);
painter.drawArc(QRectF(-radius1, -radius1, radius1*2, radius1*2), -45 * 16, 270 * 16); // 绘制渐变圆环*/
// 设置画笔颜色为蓝色
painter.setPen(QPen(Qt::blue));
// 设置填充颜色为蓝色
painter.setBrush(QBrush(Qt::blue));
double radius = 110; // 圆的半径
double interval = ((135+index*4.5) * M_PI / 180); // 角度间隔,将度数转换为弧度
double x = radius * std::cos(interval);
double y = radius * std::sin(interval);
// 计算第二个端点的坐标,距离为10
double x2 = x + 10 * std::cos(interval + M_PI / 3);
double y2 = y + 10 * std::sin(interval + M_PI / 3);
// 计算第三个端点的坐标,距离为10
double x3 = x + 10 * std::cos(interval - M_PI / 3);
double y3 = y + 10 * std::sin(interval - M_PI / 3);
// 定义三角形的顶点坐标
QPointF p1(x, y);
QPointF p2(x2, y2); // 右下角
QPointF p3(x3, y3); // 左下角
QPolygonF triangle;
triangle << p1 << p2 << p3;
painter.drawPolygon(triangle);
/*painter.setPen(halfColor);
//设置字体大小
int radius = 95;//数字显示半径值
//QFont font = painter.font();
font.setBold(true);
painter.setFont(font);
int pointSize = font.pointSize();
//painter.setPen(minuteColor); //设置文字颜色
//绘制速度单位文字 Km/h
painter.drawText(QRect(-25,-50,50,15), Qt::AlignCenter, QString("°C"));
//painter.drawText(QRectF(-20,62,40,20), Qt::AlignCenter, QString::number(index));
// 绘制文本
for (int i = 0; i < 60; ++i) {
if(i>=3 && i<=10){
continue;
}else{
if(i>=0 && i<=2)
{
//painter.drawText(textRectF(radius*0.8, pointSize, i * 45), Qt::AlignCenter, QString::number((i+12)*10));
}
else
{
if(i < 18)
{
painter.drawText(textRectF(radius*0.8, pointSize, i * 45), Qt::AlignCenter, QString::number((i-11)*10));
}
}
}
}
painter.setPen(formColor);
// 绘制内刻度线 每格间隔1.8度
for (int j = 0; j < 80; ++j) { // 此80为所有小格数 下面减去20 = 6
if(j>10 && j<30){
}else{
if ((j % 5) != 0)//半格处预留
painter.drawLine(92, 0, 96, 0);
}
painter.rotate(4.5);
}
// 设置红色刻度线
painter.setPen(maxColor);
for (int j = 0; j < 80; ++j) {
if(j>10 && j<30){
}else{
//if ((j % 5) != 0)//半格处预留
if(j>=1 && j<=9)
painter.drawLine(92, 0, 96, 0);
}
painter.rotate(4.5);
}
// 设置绿色刻度线
painter.setPen(minColor);
for (int j = 0; j < 80; ++j) {
if(j>10 && j<30){
}else{
//if ((j % 5) != 0)//半格处预留
if(j>=31 && j<=39)
painter.drawLine(92, 0, 96, 0);
}
painter.rotate(4.5);
}*/
// 设置蓝色刻度线
/*painter.setPen(formColor);
for (int j = 0; j < 200; ++j) {
if(j>10 && j<30){
}else{
//if ((j % 5) != 0)//半格处预留
if(j>=80 && j<=100)
painter.drawLine(92, 0, 96, 0);
}
painter.rotate(4.5);
}*/
// 绘制内刻度线半格刻度
/*painter.setPen(formColor);
for (int j = 0; j < 80; ++j) {
if(j>10 && j<30){
}else{
if ((j % 5) == 0 && (j % 10) != 0)
painter.drawLine(90, 0, 96, 0);//长度比最短的刻度长一点点 黑色
}
painter.rotate(4.5);
}
// 设置红色刻度线
painter.setPen(maxColor);
for (int j = 0; j < 80; ++j) {
if(j>10 && j<30){
}else{
if ((j % 5) == 0 && (j % 10) != 0)
{
if(j>=0 && j<=10)
{
painter.drawLine(90, 0, 96, 0);
}
}
}
painter.rotate(4.5);
}
// 设置绿色刻度线
painter.setPen(minColor);
for (int j = 0; j < 80; ++j) {
if(j>10 && j<30){
}else{
if((j % 5 == 0 && (j % 10) != 0))
{
if(j>=30 && j<=40)
{
painter.drawLine(90, 0, 96, 0);
}
}
}
painter.rotate(4.5);
}*/
// 设置蓝色刻度线
/*painter.setPen(formColor);
for (int j = 0; j < 80; ++j) {
if(j>10 && j<30){
}else{
if((j % 5 == 0 && (j % 10) != 0))
{
if(j>=100 && j<=200)
{
painter.drawLine(90, 0, 96, 0);
}
}
}
painter.rotate(4.5);
}*/
painter.setPen(halfColor);
//下方中部画一个圆角矩形用来显示当前速度
painter.setBrush(Qt::NoBrush);
painter.drawRoundedRect(QRectF(-50,-32,100,50),6,6);
QFont font1;
font1.setPointSize(24); // 设置字体大小为12
painter.setFont(font1);
//显示当前速度
painter.drawText(QRectF(-42,-25,80,40), Qt::AlignCenter, QString::fromUtf8("%1°C").arg(index*1));