核心代码来自 feiyangqingyun 大神链接:https://blog.csdn.net/feiyangqingyun/article/details/98586038
这边主要加上大神感觉特别简单没有贴出来的代码,并修改成四格类型的电池,同时参考书籍《Qt 5.9 C++开发指南》
致敬大神们
#ifndef QMYBATTERY_H
#define QMYBATTERY_H
#include <QWidget>
#include <QColor>
#include <QTimer>
#include <QFont>
class QmyBattery : public QWidget
{
Q_OBJECT
Q_PROPERTY(double minValue READ getMinValue WRITE setMinValue)
Q_PROPERTY(double maxValue READ getMaxValue WRITE setMaxValue)
Q_PROPERTY(double value READ getValue WRITE setValue)
Q_PROPERTY(double alarmValue READ getAlarmValue WRITE setAlarmValue)
Q_PROPERTY(double step READ getStep WRITE setStep)
Q_PROPERTY(int borderRadius READ getBorderRadius WRITE setBorderRadius)
Q_PROPERTY(int bgRadius READ getBgRadius WRITE setBgRadius)
Q_PROPERTY(int headRadius READ getHeadRadius WRITE setHeadRadius)
Q_PROPERTY(QColor borderColorStart READ getBorderColorStart WRITE setBorderColorStart)
Q_PROPERTY(QColor borderColorEnd READ getBorderColorEnd WRITE setBorderColorEnd)
Q_PROPERTY(QColor alarmColorStart READ getAlarmColorStart WRITE setAlarmColorStart)
Q_PROPERTY(QColor alarmColorEnd READ getAlarmColorEnd WRITE setAlarmColorEnd)
Q_PROPERTY(QColor normalColorStart READ getNormalColorStart WRITE setNormalColorStart)
Q_PROPERTY(QColor normalColorEnd READ getNormalColorEnd WRITE setNormalColorEnd)
Q_PROPERTY(QFont textFont READ gettextFont WRITE settextFont)
Q_PROPERTY(QColor textColor READ gettextColor WRITE settextColor)
public:
explicit QmyBattery(QWidget *parent = nullptr);
protected:
void paintEvent(QPaintEvent *);
void drawBorder(QPainter *painter);
void drawBg(QPainter *painter);
void drawHead(QPainter *painter);
void drawtext(QPainter *painter);
void drawCharge(QPainter *painter);
void drawInit();
private slots:
void updateValue();
private:
double minValue; //最小值
double maxValue; //最大值
double value; //目标电量
double alarmValue; //电池电量警戒值
double step; //每次移动的步长
int borderRadius; //边框圆角角度
int bgRadius; //背景进度圆角角度
int headRadius; //头部圆角角度
QColor borderColorStart; //边框渐变开始颜色
QColor borderColorEnd; //边框渐变结束颜色
QColor alarmColorStart; //电池低电量时的渐变开始颜色
QColor alarmColorEnd; //电池低电量时的渐变结束颜色
QColor normalColorStart; //电池正常电量时的渐变开始颜色
QColor normalColorEnd; //电池正常电量时的渐变结束颜色
bool isForward; //是否往前移
double currentValue; //当前电量
QRectF batteryRect; //电池主体区域
QTimer *timer; //绘制定时器
bool Charging;
QFont textFont;
QColor textColor;
public:
double getMinValue() const;
double getMaxValue() const;
double getValue() const;
double getAlarmValue() const;
double getStep() const;
int getBorderRadius() const;
int getBgRadius() const;
int getHeadRadius() const;
QColor getBorderColorStart() const;
QColor getBorderColorEnd() const;
QColor getAlarmColorStart() const;
QColor getAlarmColorEnd() const;
QColor getNormalColorStart() const;
QColor getNormalColorEnd() const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
QFont gettextFont()const;
QColor gettextColor()const;
signals:
void valueChanged(double value);
public slots:
//设置范围值
void setRange(double minValue, double maxValue);
void setRange(int minValue, int maxValue);
//设置最大最小值
void setMinValue(double minValue);
void setMaxValue(double maxValue);
//设置电池电量值
void setValue(double value);
void setValue(int value);
//设置电池电量警戒值
void setAlarmValue(double alarmValue);
void setAlarmValue(int alarmValue);
//设置步长
void setStep(double step);
void setStep(int step);
//设置边框圆角角度
void setBorderRadius(int borderRadius);
//设置背景圆角角度
void setBgRadius(int bgRadius);
//设置头部圆角角度
void setHeadRadius(int headRadius);
//设置边框渐变颜色
void setBorderColorStart(const QColor &borderColorStart);
void setBorderColorEnd(const QColor &borderColorEnd);
//设置电池电量报警时的渐变颜色
void setAlarmColorStart(const QColor &alarmColorStart);
void setAlarmColorEnd(const QColor &alarmColorEnd);
//设置电池电量正常时的渐变颜色
void setNormalColorStart(const QColor &normalColorStart);
void setNormalColorEnd(const QColor &normalColorEnd);
void setCharging(bool value);
// 设置百分比字体
void settextFont(const QFont &textFont);
void settextColor(const QColor &textColor);
};
#endif // QMYBATTERY_H
#include "qmybattery.h"
#include <QPainter>
#include <QDebug>
QmyBattery::QmyBattery(QWidget *parent) : QWidget(parent)
{
drawInit();
}
void QmyBattery::drawInit()
{
minValue = 0.0;
maxValue = 100.0;
value = 0.0; //目标电量
alarmValue = 30.0; //电池电量警戒值
step = 0.5; //每次移动的步长
borderRadius = 8; //边框圆角角度
bgRadius = 5; //背景进度圆角角度
headRadius = 3; //头部圆角角度
borderColorStart =QColor(100,100,100,255); //边框渐变开始颜色
borderColorEnd = QColor(80,80,80,255); //边框渐变结束颜色
alarmColorStart = QColor(250,118,113,255); //电池低电量时的渐变开始颜色
alarmColorEnd = QColor(204,38,38,255); //电池低电量时的渐变结束颜色
normalColorStart = QColor(50,205,51,255); //电池正常电量时的渐变开始颜色
normalColorEnd = QColor(60,179,133,255); //电池正常电量时的渐变结束颜色
isForward = 0; //是否往前移
currentValue = 0.0; //当前电量
Charging = false;
isForward = true;
}
void QmyBattery::drawtext(QPainter *painter)
{
painter->save();
QFontMetrics textSize(this->font());
int ipow= qRound(currentValue/(maxValue - minValue)*100);
if(ipow > 100)
{
ipow = 100;
}
else if(ipow < 0)
{
ipow = 0;
}
QString powStr =QString::number(ipow) + '%';
//qDebug()<<"powStr:"<<powStr << currentValue << maxValue - minValue<<currentValue/(maxValue - minValue)*100;
QRect textRect=textSize.boundingRect(powStr);//得到字符串的rect
painter->setFont(textFont);
painter->setPen(textColor);
//qDebug()<<textRect.width()<< textRect.height();
//qDebug()<<batteryRect.width()<< batteryRect.height();
painter->drawText(batteryRect.width()/2 - textRect.width()/2,
batteryRect.height()/2 + textRect.height(),
powStr);
painter->restore();
}
void QmyBattery::drawCharge(QPainter *painter)
{
if(Charging == false)
return;
painter->save();
//定义6个点
static const QPointF points[6] = {
QPointF(batteryRect.width() -batteryRect.width()/20, batteryRect.height()/10*2),
QPointF(batteryRect.width()-batteryRect.width()/20*3, batteryRect.height()/10*5),
QPointF(batteryRect.width()-batteryRect.width()/10, batteryRect.height()/10*5),
QPointF(batteryRect.width()-batteryRect.width()/10, batteryRect.height()/10*8),
QPointF(batteryRect.width()-batteryRect.width()/40, batteryRect.height()/10*4),
QPointF(batteryRect.width()-batteryRect.width()/20, batteryRect.height()/10*4),
};
painter->drawPolygon(points, 6);
painter->restore();
}
void QmyBattery::paintEvent(QPaintEvent *)
{
//绘制准备工作,启用反锯齿
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
//绘制边框
drawBorder(&painter);
//绘制背景
drawBg(&painter);
//绘制头部
drawHead(&painter);
drawtext(&painter);
//drawCharge(&painter);
}
void QmyBattery::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->drawRoundedRect(batteryRect, borderRadius, borderRadius);
painter->restore();
}
//void QmyBattery::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);
// }
// 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->setPen(Qt::NoPen);
// painter->setBrush(batteryGradient);
// painter->drawRoundedRect(rect, bgRadius, bgRadius);
// painter->restore();
//}
void QmyBattery::drawBg(QPainter *painter)
{
painter->save();
QLinearGradient batteryGradient(QPointF(0, 0), QPointF(0, height()));
int margin = qMin(width(), height()) / 20;
double unit = (batteryRect.width() - (margin * 2)) / 100;
double width25 = 23 * unit;
if (currentValue > 75)
{
batteryGradient.setColorAt(0.0, normalColorStart);
batteryGradient.setColorAt(1.0, normalColorEnd);
QPointF topLeft(batteryRect.topLeft().x() + margin + 3*width25 + 3*margin, batteryRect.topLeft().y() + margin);
QPointF bottomRight(4 * (width25 + margin), batteryRect.bottomRight().y() - margin);
QRectF rect(topLeft, bottomRight);
painter->setPen(Qt::NoPen);
painter->setBrush(batteryGradient);
painter->drawRoundedRect(rect, bgRadius, bgRadius);
}
if (currentValue > 50)
{
batteryGradient.setColorAt(0.0, normalColorStart);
batteryGradient.setColorAt(1.0, normalColorEnd);
QPointF topLeft(batteryRect.topLeft().x() + margin + 2*width25 + 2*margin, batteryRect.topLeft().y() + margin);
QPointF bottomRight(3*(width25 + margin), batteryRect.bottomRight().y() - margin);
QRectF rect(topLeft, bottomRight);
painter->setPen(Qt::NoPen);
painter->setBrush(batteryGradient);
painter->drawRoundedRect(rect, bgRadius, bgRadius);
}
if (currentValue > 25)
{
batteryGradient.setColorAt(0.0, normalColorStart);
batteryGradient.setColorAt(1.0, normalColorEnd);
QPointF topLeft(batteryRect.topLeft().x() + margin + width25 + margin , batteryRect.topLeft().y() + margin);
QPointF bottomRight(2*(width25 + margin), batteryRect.bottomRight().y() - margin);
QRectF rect(topLeft, bottomRight);
painter->setPen(Qt::NoPen);
painter->setBrush(batteryGradient);
painter->drawRoundedRect(rect, bgRadius, bgRadius);
}
if (currentValue >= 0)
{
if(currentValue > 25)
{
batteryGradient.setColorAt(0.0, normalColorStart);
batteryGradient.setColorAt(1.0, normalColorEnd);
}
else {
batteryGradient.setColorAt(0.0, alarmColorStart);
batteryGradient.setColorAt(1.0, alarmColorEnd);
}
QPointF topLeft(batteryRect.topLeft().x() + margin+1, batteryRect.topLeft().y() + margin);
QPointF bottomRight(width25 + margin, batteryRect.bottomRight().y() - margin);
QRectF rect(topLeft, bottomRight);
painter->setPen(Qt::NoPen);
painter->setBrush(batteryGradient);
painter->drawRoundedRect(rect, bgRadius, bgRadius);
}
painter->restore();
}
void QmyBattery::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();
}
double QmyBattery::getMinValue()const
{
return minValue;
}
double QmyBattery::getMaxValue()const
{
return maxValue;
}
double QmyBattery::getValue()const
{
return currentValue;
}
double QmyBattery::getAlarmValue()const
{
return alarmValue;
}
double QmyBattery::getStep()const
{
return step;
}
int QmyBattery::getBorderRadius()const
{
return borderRadius;
}
int QmyBattery::getBgRadius()const
{
return bgRadius;
}
int QmyBattery::getHeadRadius()const
{
return headRadius;
}
QColor QmyBattery::getBorderColorStart()const
{
return borderColorStart;
}
QColor QmyBattery::getBorderColorEnd()const
{
return borderColorEnd;
}
QColor QmyBattery::getAlarmColorStart()const
{
return alarmColorStart;
}
QColor QmyBattery::getAlarmColorEnd()const
{
return alarmColorEnd;
}
QColor QmyBattery::getNormalColorStart()const
{
return normalColorStart;
}
QColor QmyBattery::getNormalColorEnd()const
{
return normalColorEnd;
}
QSize QmyBattery::sizeHint()const
{
int H=this->height();
int W=this->width();
QSize size(W,H);
return size;
}
QSize QmyBattery::minimumSizeHint()const
{
int H=this->height();
int W=this->width();
QSize size(W,H);
return size;
}
//设置范围值
void QmyBattery::setRange(double minValue, double maxValue)
{
this->minValue = minValue;
this->maxValue = maxValue;
}
void QmyBattery::setRange(int minValue, int maxValue)
{
this->minValue = minValue;
this->maxValue = maxValue;
}
//设置最大最小值
void QmyBattery::setMinValue(double minValue)
{
this->minValue = minValue;
}
void QmyBattery::setMaxValue(double maxValue)
{
this->maxValue = maxValue;
}
//设置电池电量值
void QmyBattery::setValue(double value)
{
this->value = value;
if(isForward == false)
{
currentValue = value;
update();
}
else
{
update();
QTimer::singleShot(10, this, SLOT(updateValue()));
}
}
void QmyBattery::setValue(int value)
{
this->value = value;
if(isForward == false)
{
currentValue = value;
update();
}
else
{
update();
QTimer::singleShot(10, this, SLOT(updateValue()));
}
}
//设置电池电量警戒值
void QmyBattery::setAlarmValue(double alarmValue)
{
this->alarmValue = alarmValue;
}
void QmyBattery::setAlarmValue(int alarmValue)
{
this->alarmValue = alarmValue;
}
//设置步长
void QmyBattery::setStep(double step)
{
this->step = step;
}
void QmyBattery::setStep(int step)
{
this->step = step;
}
//设置边框圆角角度
void QmyBattery::setBorderRadius(int borderRadius)
{
this->borderRadius = borderRadius;
update();
}
//设置背景圆角角度
void QmyBattery::setBgRadius(int bgRadius)
{
this->bgRadius = bgRadius;
update();
}
//设置头部圆角角度
void QmyBattery::setHeadRadius(int headRadius)
{
this->headRadius = headRadius;
update();
}
//设置边框渐变颜色
void QmyBattery::setBorderColorStart(const QColor &borderColorStart)
{
this->borderColorStart = borderColorStart;
update();
}
void QmyBattery::setBorderColorEnd(const QColor &borderColorEnd)
{
this->borderColorEnd = borderColorEnd;
update();
}
//设置电池电量报警时的渐变颜色
void QmyBattery::setAlarmColorStart(const QColor &alarmColorStart)
{
this->alarmColorStart = alarmColorStart;
update();
}
void QmyBattery::setAlarmColorEnd(const QColor &alarmColorEnd)
{
this->alarmColorEnd = alarmColorEnd;
update();
}
//设置电池电量正常时的渐变颜色
void QmyBattery::setNormalColorStart(const QColor &normalColorStart)
{
this->normalColorStart = normalColorStart;
update();
}
void QmyBattery::setNormalColorEnd(const QColor &normalColorEnd)
{
this->normalColorEnd = normalColorEnd;
update();
}
void QmyBattery::setCharging(bool value)
{
this->Charging = value;
update();
}
QFont QmyBattery::gettextFont()const
{
return this->textFont;
}
void QmyBattery::settextFont(const QFont &textFont)
{
this->textFont = textFont;
update();
}
QColor QmyBattery::gettextColor()const
{
return textColor;
}
void QmyBattery::settextColor(const QColor &textColor)
{
this->textColor = textColor;
update();
}
void QmyBattery::updateValue()
{
if(value >= currentValue + step)
{
currentValue += step;
update();
QTimer::singleShot(10, this, SLOT(updateValue()));
}
else if(value <= currentValue - step)
{
currentValue -= step;
update();
QTimer::singleShot(10, this, SLOT(updateValue()));
}
}
代码下载地址:https://download.csdn.net/download/wang112031/12033697