QT第六天

要求:使用QT绘图,完成仪表盘绘制,如下图。

QT第六天_第1张图片

素材

 QT第六天_第2张图片

 运行效果:

QT第六天_第3张图片QT第六天_第4张图片

代码: 

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    void paintEvent(QPaintEvent *event) override;

public slots:
    void horizontalSliderValueChangedSlot(int);

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

 widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->horizontalSlider->setValue(0);
    ui->horizontalSlider->setMaximum(100);
    ui->horizontalSlider->setMinimum(0);

    connect(ui->horizontalSlider, SIGNAL(valueChanged(int) ), this, SLOT(horizontalSliderValueChangedSlot(int)));
}

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

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter p(this);

    double R  = this->width()*0.375;

    p.translate(this->width()/2,this->height()/2);


    p.drawPixmap(-R*1.01,-R*0.98,2*R,2*R,QPixmap(":/rs/Pan.png"));

    p.rotate(ui->horizontalSlider->value() * 2.8 + 130);
    p.drawPixmap(-R*0.08,-R*0.085,R*0.72,R*0.15,QPixmap(":/rs/Point.png"));

}

void Widget::horizontalSliderValueChangedSlot(int)
{
    this->update();
}

widget.ui



 Widget
 
  
   
    0
    0
    400
    400
   
  
  
   
    400
    400
   
  
  
   
    0
    0
   
  
  
   
    0
    0
   
  
  
   仪表盘
  
  
   
    100
   
   
    10
   
   
    100
   
   
    10
   
   
    
     
      Qt::Vertical
     
     
      
       20
       40
      
     
    
   
   
    
     
      Qt::Horizontal
     
    
   
  
 
 
 

你可能感兴趣的:(QT,qt,开发语言,linux,c++)