图形绘制-仪表盘(4)

本章节我们介绍如何绘制实时变化的车速及指针对应的位置。

效果如下:

关于通过继承重写QWidget的绘制事件paintEvent(),来绘制仪表盘的基础操作,请看

图形绘制-仪表盘(1)-CSDN博客

图形绘制-仪表盘(2)-CSDN博客

图形绘制-仪表盘(3)-CSDN博客

完整代码,请从https://download.csdn.net/download/zhouhui1982/88794418下载。

在dashboard.h头文件,声明公有函数setSpeed,用于设置速度

public slots:

void setSpeed(int speed);

在dashboard.cpp代码文件中实现:

void Dashboard::setSpeed(int speed)

{

//超出范围判断

if(speed>m_maxSpeed)

m_curSpeed=m_maxSpeed;

else if(speed

speed=m_minSpeed;

else

m_curSpeed = speed;

//刷新绘制消息,很重要,不加不重新绘制

update();

}

在调用仪表盘的主窗体mainwindow.cpp代码中加定时器,通过随机函数,得到100以内的整数,赋值给仪表盘:

加引用头文件:

#include

在构造函数中加以下代码:

QTimer *timerSpeed=new QTimer(this);

connect(timerSpeed,&QTimer::timeout,={

int speed = rand()%100;

dashboard->setSpeed(speed);

qDebug()

});

timerSpeed->start(100);

相关推荐
xcyxiner14 小时前
DicomViewer (后台线程处理文件)4
qt
xcyxiner21 小时前
DicomViewer (添加模型类)3
qt
xcyxiner2 天前
DicomViewer (目录调整) 2
qt
xcyxiner2 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
clint4563 天前
C++进阶(1)——前景提要
c++
夜悊3 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴3 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt0013 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
玖玥拾4 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
桥田智能4 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构