一、准备工作
我这里加了表示温湿度的仪表盘控件,准备了指针图片、温度盘图片和湿度盘图片
在这里需要注意的是仪表盘和指针背景要透明
二、调用的主要程序
下面是可以直接调用的头文件和程序
下面是.h文件
#ifndef CBMPPOINTER_H
#define CBMPPOINTER_H
#include "qpushbutton.h"
#include "QPainter"
class CBmpPointer : public QPushButton
{
public:
CBmpPointer(QPixmap *pImage, const QPoint &ptOrigin, const QRect &rtArea, QWidget *parent = Q_NULLPTR);
~CBmpPointer();
void SetAngle(int nAngleSpace,int nPointOffset);
void SetMinMaxData(int nMinData,int nMaxData);
void SetNum(int nCurNum);
private:
QPixmap *m_pImg;
QPoint m_ptstart;
int m_nAngleSpace;
int m_nPointOffset;
int m_nMinData;
int m_nMaxData;
int m_nCurData;
protected:
virtual void paintEvent(QPaintEvent *);
};
#endif // CBMPPOINTER_H
下面是调用的程序
#include "cbmppointer.h"
CBmpPointer::CBmpPointer(QPixmap *pImage, const QPoint &ptOrigin, const QRect &rtArea, QWidget *parent)
:QPushButton(parent)
{
m_pImg = pImage;
m_ptstart.setX(0 - ptOrigin.x());
m_ptstart.setY(0 - ptOrigin.y());
move(QPoint(rtArea.left(), rtArea.top()));
resize(rtArea.size());
}
CBmpPointer::~CBmpPointer()
{
m_pImg = NULL;
}
void CBmpPointer::SetAngle(int nAngleSpace,int nPointOffset)
{
m_nAngleSpace = nAngleSpace;
m_nPointOffset = nPointOffset;
}
void CBmpPointer::SetMinMaxData(int nMinData,int nMaxData)
{
m_nMinData = nMinData;
m_nMaxData = nMaxData;
}
void CBmpPointer::SetNum(int nCurNum)
{
if((nCurNum > m_nMaxData) || (nCurNum < m_nMinData))
{
return;
}
m_nCurData = nCurNum;
update();
}
void CBmpPointer::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
painter.translate(width()/2, height()/2);
int rorAngle = m_nPointOffset + ((float)m_nAngleSpace /(float)(m_nMaxData -m_nMinData)) * (float)(m_nCurData - m_nMinData);
painter.rotate(rorAngle);
painter.drawPixmap(m_ptstart, *m_pImg);
}
三、在自己的工程文件中加的程序
在头文件里加的程序
QPixmap *m_pBGFengli; //风力图片
QPixmap *m_pBGWenDu; //温度图片
QPixmap *m_pBGShiDu; //湿度图片
QPixmap *m_pZhiZhenFengLi;
QPixmap *m_pZhiZhenWenDu;
QPixmap *m_pZhiZhenShiDu;
CBmpPointer *m_pFengLi; //风力仪表盘功能类
CBmpPointer *m_pWenDu; //温度仪表盘功能类
CBmpPointer *m_pShiDu; //湿度仪表盘功能类
void AppInit(); //一些初始化步骤
void paintEvent(QPaintEvent*event);
QTimer *tm;
int nTest = 0;
在.cpp文件中需要添加的程序
AppInit();
tm = new QTimer;
connect(tm, SIGNAL(timeout()), this, SLOT(slot_Test()));
tm->start(80);
pix.load(":/images/湿度.png");
pix=pix.scaled(pix.width()*0.7, pix.height()*0.7);
painter.drawPixmap(QRect(QPoint(230,400), QSize(pix.width(),pix.height())),pix);
pix.load(":/images/温度.png");
pix=pix.scaled(pix.width()*0.7, pix.height()*0.7);
painter.drawPixmap(QRect(QPoint(240,200), QSize(pix.width(),pix.height())),pix);
pix.load(":/images/风力.png");
pix=pix.scaled(pix.width()*0.7, pix.height()*0.7);
painter.drawPixmap(QRect(QPoint(30,200), QSize(pix.width(),pix.height())),pix);
void Widget::slot_Test()
{
int nMax = 100;
nTest++;
if(nTest >= nMax)
{
nTest = 0;
}
// m_pShiDu->SetNum(nTest);
m_pWenDu->SetNum(nTest);
//m_pFengLi->SetNum(nTest);
}
void Widget::AppInit()
{
//把电脑里准备的图片路径给给个QPixmap,根据你自己实际图片路径填写
m_pBGFengli = new QPixmap(":/images/风力.png");
m_pBGWenDu = new QPixmap(":/images/温度.png");
m_pBGShiDu = new QPixmap(":/images/湿度.png");
QPixmap pix;
pix.load(":/images/pointer.png");
pix=pix.scaled(pix.width()*0.7, pix.height()*0.7);
m_pZhiZhenFengLi = new QPixmap(pix);
m_pZhiZhenWenDu = new QPixmap(pix);
m_pZhiZhenShiDu = new QPixmap(pix);
int nWidth = 270;
int nHeight = 356-88;
int xShiDu = 210;
int yShiDu = 356;
int xFengLi=5;
int yFengLi=160;
int xwenDu = 210;
int ywenDu = 160;
m_pFengLi = new CBmpPointer(m_pZhiZhenFengLi,QPoint(56,4),QRect(QPoint(xFengLi,yFengLi),QPoint(xFengLi+ nWidth,yFengLi+nHeight)),this);
m_pWenDu = new CBmpPointer(m_pZhiZhenWenDu,QPoint(56,4),QRect(QPoint(xwenDu,ywenDu),QPoint(xwenDu+ nWidth,ywenDu+ nWidth)),this);
m_pShiDu = new CBmpPointer(m_pZhiZhenShiDu,QPoint(56,4),QRect(QPoint(xShiDu,yShiDu),QPoint(xShiDu + nWidth,yShiDu+nWidth)),this);
m_pFengLi->SetMinMaxData(0,12);
m_pWenDu->SetMinMaxData(-50,50); //设置仪表盘的刻度上下限
m_pShiDu->SetMinMaxData(0,100);
m_pFengLi->SetAngle(218,5);
m_pWenDu->SetAngle(186,0); //第一个参数设置角度空间范围大小,第二参数是基于0的偏移值,0位于9点钟方向
m_pShiDu->SetAngle(186,20);
m_pFengLi->SetNum(100);
m_pWenDu->SetNum(0);
m_pShiDu->SetNum(100);
}