QT绘制仪表盘

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include<QPainter>
#include<QRect>
#include<QSlider>

#include"diawidget.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();

public slots:
    void sliderSlot(int);
private:
    Ui::Dialog *ui;
    Diawidget *dia;
    QSlider *slider;
    int scale;
    QLabel *label;
    QString str;
};
#endif // DIALOG_H

diawidget.h

#ifndef DIAWIDGET_H
#define DIAWIDGET_H

#include <QWidget>
#include<QPainter>
#include<QPushButton>
#include<QDebug>
#include<QPainterPath>
#include<QPointF>
#include<complex>
#include<qmath.h>
#include<QFont>
#include<QLabel>
class Diawidget : public QWidget
{
    Q_OBJECT
public:
    explicit Diawidget(QWidget *parent = nullptr);

    void paintEvent(QPaintEvent *enent);
    void paintWatchHand(QPainter *painter);
    void paintScale(QPainter &painter);
    int scale=-120;
signals:

private:
    void drawBg(QPainter *painter);

    int radius;
};

#endif // DIAWIDGET_H

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
setFixedSize(640,480);

    slider = new QSlider(this);
    slider->setGeometry(QRect(10,10,30,200));
    slider->setRange(-120,120);
    slider->setValue(-120);
    connect(slider,SIGNAL(valueChanged(int)),this,SLOT(sliderSlot(int)));

    dia = new Diawidget(this);
    dia->setGeometry(QRect(300,100,250,250));
    dia->show();

    str = "当前速度:";
    label = new QLabel(this);
    label->setGeometry(QRect(480,10,150,40));
    label->setStyleSheet("QLabel{"
                         "background-color:'#000000';"
                         "color:rgb(98,246,255);}" );
}

Dialog::~Dialog()
{
    delete ui;
}



void Dialog::sliderSlot(int a)
{

    dia->scale = a;
    dia->repaint();

    QFont font;
    font.setBold(10);
    font.setItalic(10);
    font.setPointSize(10);
    font.setFamily("Microsoft YaHei");

    label->setFont(font);
    label->setText(str+QString::number(a+120)+"km/h");
}

diawidget.h

#include "diawidget.h"

Diawidget::Diawidget(QWidget *parent) : QWidget(parent)
{
    this->setFixedSize(300,300);
}

void Diawidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    int w = 270;
    int h= 270;
    //qDebug()<<w<<h;
    radius = qMin(w,h)/2;
    painter.setRenderHint(QPainter::Antialiasing); //抗锯齿
    painter.translate(w/2+5,h/2+5);

    drawBg(&painter);
    paintScale(painter);
    paintWatchHand(&painter);
}

void Diawidget::paintWatchHand(QPainter *painter)   //画仪表指针
{
    painter->setPen(Qt::NoPen);
    painter->setBrush(Qt::red);
    QPainterPath pointPath;
    pointPath.moveTo(-5,-5);
    pointPath.lineTo(0,-100);
    pointPath.lineTo(5,-5);
    pointPath.lineTo(5,5);
    pointPath.lineTo(0,10);
    pointPath.lineTo(-5,5);
    painter->rotate(scale);
    painter->drawPath(pointPath);
}

void Diawidget::paintScale(QPainter &painter)    //画仪表盘数字刻度
{

    QFont font;
    font.setPointSize(10);
    font.setFamily("Microsoft YaHei");
    painter.setFont(font);
    painter.setPen(QColor(98,246,255));
    for(int i=0;i<13;i++)
    {
        QPointF point(0,0);
        painter.save();
        point.setX((radius-5)*qCos(((210-i*20)*M_PI)/180));
        point.setY((radius-5)*qSin(((210-i*20)*M_PI)/180));
        painter.translate(point.x(),-point.y());
        painter.rotate(-120+i*20);
        painter.drawText(-14,0,30,20,Qt::AlignCenter,QString::number(i*20));
        painter.restore();
    }
    QFont font1;
    font1.setPointSize(15);
    font1.setFamily("Microsoft YaHei");
    font1.setItalic(10);
    font1.setBold(10);
    painter.setPen(QColor(98,246,255));
    painter.setFont(font1);
    painter.drawText(-30,50,tr("km/h"));
}

void Diawidget::drawBg(QPainter *painter)// 画仪表刻度线
{
    int r = radius*0.8;
    painter->setBrush(Qt::black);
    painter->drawEllipse(-1.25*r,-1.25*r,r*2.5,r*2.5);

    painter->setPen(Qt::NoPen);
    painter->setBrush(QColor(98,246,255));
    QPainterPath pointPath;
    pointPath.moveTo(-0.5,-0.5);
    pointPath.lineTo(0.5,-0.5);
    pointPath.lineTo(0.5,8);
    pointPath.lineTo(-0.5,8);
    for(int i=0;i<13;i++)
    {
        QPointF point(0,0);
        painter->save();
        point.setX((radius)*qCos(((210-i*20)*M_PI)/180));
        point.setY((radius)*qSin(((210-i*20)*M_PI)/180));
        painter->translate(point.x(),-point.y());
        painter->rotate(-120+i*20);
        painter->drawPath(pointPath);
        painter->restore();
    }

    QPainterPath pointPath1;
    pointPath1.moveTo(-0.5,-0.5);
    pointPath1.lineTo(0.5,-0.5);
    pointPath1.lineTo(0.5,4);
    pointPath1.lineTo(-0.5,4);
    for(int i=0;i<120;i++)
    {
        QPointF point(0,0);
        painter->save();
        point.setX(radius*qCos(((210-i*2)*M_PI)/180));
        point.setY(radius*qSin(((210-i*2)*M_PI)/180));
        painter->translate(point.x(),-point.y());
        painter->rotate(-120+i*2);
        painter->drawPath(pointPath1);
        painter->restore();
    }
}

main.c

#include "dialog.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    return a.exec();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值