资源简介
模拟电量进度条
模拟电量进度条

代码片段和文件信息
#pragma execution_character_set(“utf-8“)
#include “battery.h“
#include “qpainter.h“
#include “qtimer.h“
#include “qdebug.h“
Battery::Battery(QWidget *parent) : QWidget(parent)
{
minValue = 0;
maxValue = 100;
value = 0;
alarmValue = 30;
step = 0.5;
borderColorStart = QColor(100 100 100);
borderColorEnd = QColor(80 80 80);
alarmColorStart = QColor(250 118 113);
alarmColorEnd = QColor(204 38 38);
normalColorStart = QColor(50 205 51);
normalColorEnd = QColor(60 179 133);
isForward = false;
currentValue = 0;
timer = new QTimer(this);
timer->setInterval(10);
connect(timer SIGNAL(timeout()) this SLOT(updateValue()));
}
Battery::~Battery()
{
if (timer->isActive()) {
timer->stop();
}
}
void Battery::paintEvent(QPaintEvent *)
{
//绘制准备工作启用反锯齿
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
//绘制边框
drawBorder(&painter);
//绘制背景
drawBg(&painter);
}
void Battery::drawBorder(QPainter *painter)
{
painter->save();
double headWidth = width() / 10;
double batteryWidth = width() - headWidth;
//绘制电池边框
QPointF topLeft(5 5);
QPointF bottomRight(batteryWidth height() - 5);
batteryRect = QRectF(topLeft bottomRight);
painter->setPen(QPen(borderColorStart 5));
painter->setBrush(Qt::NoBrush);
painter->drawRoundRect(batteryRect 10 20);
//绘制电池头部
QPointF headRectTopLeft(batteryRect.topRight().x() height() / 3);
QPointF headRectBottomRight(width() height() - height() / 3);
QRectF headRect(headRectTopLeft headRectBottomRight);
QLinearGradient headRectGradient(headRect.topLeft() headRect.bottomLeft());
headRectGradient.setColorAt(0.0 borderColorStart);
headRectGradient.setColorAt(1.0 borderColorEnd);
painter->setBrush(headRectGradient);
painter->drawRoundRect(headRect 15 25);
painter->restore();
}
void Battery::drawBg(QPainter *painter)
{
painter->save();
QLinearGradient batteryGradient(QPointF(0 0) QPointF(0 height()));
if (currentValue <= alarmValue) {
batteryGradient.setColorAt(0.0 alarmColorStart);
batteryGradient.setColorAt(1.0 alarmColorEnd);
} else {
batteryGradient.setColorAt(0.0 normalColorStart);
batteryGradient.setColorAt(1.0 normalColorEnd);
}
painter->setPen(Qt::NoPen);
painter->setBrush(batteryGradient);
int margin = qMin(width() height()) / 20;
double unit = (batteryRect.width() - (margin * 2)) / 100;
double width = currentValue * unit;
QPointF topLeft(batteryRect.topLeft().x() + margin batteryRect.topLeft().y() + margin);
QPointF bottomRight(width + margin + 5 batteryRect.bottomRight().y() - margin);
QRectF rect(topLeft bottomRight);
painter->drawRoundRect(rect 10 10);
painter->
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-12-28 18:05 battery\
文件 7349 2017-02-07 17:30 battery\battery.cpp
文件 4471 2017-02-10 17:11 battery\battery.h
文件 484 2017-02-08 09:47 battery\battery.pro
文件 23782 2017-12-28 18:05 battery\battery.pro.user
文件 8299 2017-01-11 10:18 battery\flatui.cpp
文件 1848 2017-02-08 09:42 battery\flatui.h
文件 382 2017-01-06 21:21 battery\frmbattery.cpp
文件 297 2017-01-06 21:17 battery\frmbattery.h
文件 1554 2017-01-06 21:20 battery\frmbattery.ui
文件 183 2017-02-08 09:46 battery\main.cpp
相关资源
- Qt界面外观
- [嵌入式Linux项目实战开发]基于QT4.7.
- Qt GUI 界面通用系统模板 源码
- 网上的一款QT界面设计的电子地图
- 二维码解析
- linux下关于Qt界面的摄像头v4l2操作源码
- 360UI完美界面
- 净化设备管理
- 基于qt界面的语音交互软件
- 综合组件的界面
- opencv打开图片并显示在Qt界面上
- Qt360 界面
- 动态绘制曲线
- QtitanRibbon3.zip
- VS+QT环境下读取excel文件到QT界面,并
- 基于嵌入式QT界面GPS定位系统
- [嵌入式Linux项目实战开发]基于QT4.7.
- 物联网 智能家居 Qt界面
- 将QT界面做成dll在qt其他程序中调用
-
将xm
l内容显示Qt界面 - DS18B20测温及QT界面实现
- qt界面跳转切换
- 基于v4l的视频采集显示程序。外加Q
- QT计算器界面和计算器功能实现
- qt界面连接wifi
- myseria串口
- 基于Qt界面显示的温湿度检测
- Qt界面多线程opencv调用摄像头
- 智慧大棚QT界面设计
- Qt高仿网易云音乐界面源码
评论
共有 条评论