QT绘制仪表盘

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include 
#include
#include
#include

#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 
#include
#include
#include
#include
#include
#include
#include
#include
#include
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()<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 

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

QT绘制仪表盘_第1张图片

 

你可能感兴趣的:(qt,ui,开发语言)