qt5实现表盘的旋转效果,通过提升QLabel类

因为工作需要,需要实现温度的表盘展示效果

实现思路:

   通过提示声QLabel控价类,实现报盘的旋转和展示效果

1. 编写一个QLabel的类MyQLabel,实现两个方法

   1.  void paintEvent(QPaintEvent *event); //重绘函数

   2.  void valueChanged(int value); //更改值

2.提升QLabel控件,实现两个方法函数的重置入

3. 通过按钮和滑动条,改变数值,实现指针的转动调整指针的指向

实现表盘转动的功能。

myqlabel.h

#ifndef MYQLABEL_H

#define MYQLABEL_H

#include

#include

#include

class MyQLabel : public QLabel

{

Q_OBJECT

public:

QPixmap needle; //指针

QPixmap overlay; //中间显示盘

QPixmap img; //显示转盘

int nvalue;

explicit MyQLabel(QWidget *parent=0);

void paintEvent(QPaintEvent *event); //重绘函数

void valueChanged(int value); //更改值

//void DrawRangle(int x ,int y ,int h,int w); //绘制矩形

};

#endif // MYQLABEL_H

2. myqlabel.c文件内容
#include "myqlabel.h"
#include 
MyQLabel::MyQLabel(QWidget *parent): QLabel(parent)
{
    needle = QPixmap(":/image/ned.png");
    overlay= QPixmap(":/image/lay.png");
    img = QPixmap(":/image/img.png");
    nvalue=-128;
}
void MyQLabel::valueChanged(int value)
{
        nvalue = value;
        this->update();
}
void MyQLabel::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.save();//保存
    painter.setRenderHint(QPainter::SmoothPixmapTransform, true); //平滑像素图,防止图形走样
    painter.translate(this->width() / 2,this->height() / 2); // 原点定位在中间位置
    qDebug()<<"1.width:"<width()/2 <<"height:"<height()/2 <width() / 2,this->height()/8*5); // 原点定位在中间位置
    qDebug()<<"3.定位点.width:"<width() / 2 <<"height:"<height() /8*5< 
  

3. widget.h 文件

#include

#include

#include

#include

#include

#include

namespace Ui {

class Widget;

}

class Widget : public QWidget

{

Q_OBJECT

public:

explicit Widget(QWidget *parent = 0);

~Widget();

private slots:

void on_pushButton_clicked();

void on_slider_valueChanged(int value);

private:

 Ui::Widget *ui;

};

#endif // WIDGET_H

4. widget.cpp

#include "ui_widget.h"

#include

#include

Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)

{

      ui->setupUi(this);

     resize(600,600); //设置窗体大小

}

//析构函数

Widget::~Widget()

{

delete ui;

}

void Widget::on_pushButton_clicked()

{

int evalue=QInputDialog::getInt(this,tr("输入温度数值"),tr("请输入一个对应的温度值"),0,-128,128,1);

ui->label->valueChanged(evalue);

}

//值变化时

void Widget::on_slider_valueChanged(int value)

{

qDebug()<<"value="<

ui->label->valueChanged(value);

}

5. main.cpp

#include "widget.h"

#include

int main(int argc, char *argv[])

{

QApplication a(argc, argv);

Widget w;

w.show();

return a.exec();

}

6. widget.ui

这个界面中放置一个QLabel控价,到时提升下控件即可

最终实现效果如下

qt5实现表盘的旋转效果,通过提升QLabel类_第1张图片

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