#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;
animation = true;
animationStep = 0.5;
borderWidth = 5;
borderRadius = 8;
bgRadius = 5;
headRadius = 3;
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);
//绘制头部
drawHead(&painter);
}
void Battery::drawBorder(QPainter *painter)
{
painter->save();
double headWidth = width() / 15;
double batteryWidth = width() - headWidth;
//绘制电池边框
QPointF topLeft(borderWidth, borderWidth);
QPointF bottomRight(batteryWidth, height() - borderWidth);
batteryRect = QRectF(topLeft, bottomRight);
painter->setPen(QPen(borderColorStart, borderWidth));
painter->setBrush(Qt::NoBrush);
painter->drawRoundedRect(batteryRect, borderRadius, borderRadius);
painter->restore();
}
void Battery::drawBg(QPainter *painter)
{
if (value == minValue) {
return;
}
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);
}
int margin = qMin(width(), height()) / 20;
double unit = (batteryRect.width() - (margin * 2)) / (maxValue - minValue);
double width = currentValue * unit;
QPointF topLeft(batteryRect.topLeft().x() + margin, batteryRect.topLeft().y() + margin);
QPointF bottomRight(width + margin + borderWidth, batteryRect.bottomRight().y() - margin);
QRectF rect(topLeft, bottomRight);
painter->setPen(Qt::NoPen);
painter->setBrush(batteryGradient);
painter->drawRoundedRect(rect, bgRadius, bgRadius);
painter->restore();
}
void Battery::drawHead(QPainter *painter)
{
painter->save();
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->setPen(Qt::NoPen);
painter->setBrush(headRectGradient);
painter->drawRoundedRect(headRect, headRadius, headRadius);
painter->restore();
}
void Battery::updateValue()
{
if (isForward) {
currentValue -= animationStep;
if (currentValue <= value) {
currentValue = value;
timer->stop();
}
} else {
currentValue += animationStep;
if (currentValue >= value) {
currentValue = value;
timer->stop();
}
}
this->update();
}
QSize Battery::sizeHint() const
{
return QSize(150, 80);
}
QSize Battery::minimumSizeHint() const
{
return QSize(30, 10);
}
void Battery::setRange(double minValue, double maxValue)
{
//如果最小值大于或者等于最大值则不设置
if (minValue >= maxValue) {
return;
}
this->minValue = minValue;
this->maxValue = maxValue;
//如果目标值不在范围值内,则重新设置目标值
//值小于最小值则取最小值,大于最大值则取最大值
if (value < minValue) {
setValue(minValue);
} else if (value > maxValue) {
setValue(maxValue);
}
this->update();
}
void Battery::setRange(int minValue, int maxValue)
{
setRange((double)minValue, (double)maxValue);
}
double Battery::getMinValue() const
{
return this->minValue;
}
void Battery::setMinValue(double minValue)
{
setRange(minValue, maxValue);
}
double Battery::getMaxValue() const
{
return this->maxValue;
}
void Battery::setMaxValue(double maxValue)
{
setRange(minValue, maxValue);
}
double Battery::getValue() const
{
return this->value;
}
void Battery::setValue(double value)
{
//值和当前值一致则无需处理
if (value == this->value) {
return;
}
//值小于最小值则取最小值,大于最大值则取最大值
if (value < minValue) {
value = minValue;
} else if (value > maxValue) {
value = maxValue;
}
if (value > currentValue) {
isForward = false;
} else if (value < currentValue) {
isForward = true;
} else {
this->value = value;
this->update();
return;
}
this->value = value;
Q_EMIT valueChanged(value);
if (animation) {
timer->stop();
timer->start();
} else {
this->currentValue = value;
this->update();
}
}
void Battery::setValue(int value)
{
setValue((double)value);
}
double Battery::getAlarmValue() const
{
return this->alarmValue;
}
void Battery::setAlarmValue(double alarmValue)
{
if (this->alarmValue != alarmValue) {
this->alarmValue = alarmValue;
this->update();
}
}
void Battery::setAlarmValue(int alarmValue)
{
setAlarmValue((double)alarmValue);
}
bool Battery::getAnimation() const
{
return this->animation;
}
void Battery::setAnimation(bool animation)
{
if (this->animation != animation) {
this->animation = animation;
this->update();
}
}
double Battery::getAnimationStep() const
{
return this->animationStep;
}
void Battery::setAnimationStep(double animationStep)
{
if (this->animationStep != animationStep) {
this->animationStep = animationStep;
this->update();
}
}
int Battery::getBorderWidth() const
{
return this->borderWidth;
}
void Battery::setBorderWidth(int borderWidth)
{
if (this->borderWidth != borderWidth) {
this->borderWidth = borderWidth;
this->update();
}
}
int Battery::getBorderRadius() const
{
return this->borderRadius;
}
void Battery::setBorderRadius(int borderRadius)
{
if (this->borderRadius != borderRadius) {
this->borderRadius = borderRadius;
this->update();
}
}
int Battery::getBgRadius() const
{
return this->bgRadius;
}
void Battery::setBgRadius(int bgRadius)
{
if (this->bgRadius != bgRadius) {
this->bgRadius = bgRadius;
this->update();
}
}
int Battery::getHeadRadius() const
{
return this->headRadius;
}
void Battery::setHeadRadius(int headRadius)
{
if (this->headRadius != headRadius) {
this->headRadius = headRadius;
this->update();
}
}
QColor Battery::getBorderColorStart() const
{
return this->borderColorStart;
}
void Battery::setBorderColorStart(const QColor &borderColorStart)
{
if (this->borderColorStart != borderColorStart) {
this->borderColorStart = borderColorStart;
this->update();
}
}
QColor Battery::getBorderColorEnd() const
{
return this->borderColorEnd;
}
void Battery::setBorderColorEnd(const QColor &borderColorEnd)
{
if (this->borderColorEnd != borderColorEnd) {
this->borderColorEnd = borderColorEnd;
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
基于Qt和C++实现的电池电量控件+源码,适合期末大作业、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~ * 电池电量控件 * 1. 可设置电池电量,动态切换电池电量变化。 * 2. 可设置电池电量警戒值。 * 3. 可设置电池电量正常颜色和报警颜色。 * 4. 可设置边框渐变颜色。 * 5. 可设置电量变化时每次移动的步长。 * 6. 可设置边框圆角角度、背景进度圆角角度、头部圆角角度。 用法链接:https://blog.csdn.net/cs1395293598/article/details/137112511?spm=1001.2014.3001.5501 微博主页:https://menghui666.blog.csdn.net/
资源推荐
资源详情
资源评论
收起资源包目录
battery.zip (7个子文件)
battery
main.cpp 846B
frmbattery.cpp 443B
battery.pro 373B
battery.cpp 9KB
frmbattery.ui 1KB
battery.h 5KB
frmbattery.h 323B
共 7 条
- 1
资源评论
梦回阑珊
- 粉丝: 2936
- 资源: 798
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功