QTbattery

#ifndef WIDGET_H

#define WIDGET_H

#include <QWidget>

#include <QPainter>

#include <QTimer>

QT_BEGIN_NAMESPACE

namespace Ui { class Widget; }

QT_END_NAMESPACE

class Widget : public QWidget

{

Q_OBJECT

public:

Widget(QWidget *parent = nullptr);

~Widget();

private:

Ui::Widget *ui;

QRectF batteryRect;

QTimer *inputTimer;

double _currentValue;

int _margin;

double _minValue;

double _maxValue;

bool _isForward;

int _batteryWidth;

int _batteryHeight;

protected:

void paintEvent(QPaintEvent *);

void drawBorder(QPainter *painter);

void drawBackground(QPainter *painter);

void drawText(QPainter *painter);

private slots:

void inputValue();

};

#endif // WIDGET_H

#include "widget.h"

#include "ui_widget.h"

Widget::Widget(QWidget *parent)

: QWidget(parent)

,_currentValue(10)

,_margin(3)

,_minValue(0)

,_maxValue(100)

,_isForward(true)

,_batteryWidth(50)

,_batteryHeight(20)

, ui(new Ui::Widget)

{

ui->setupUi(this);

setFixedSize(350,180);

setBaseSize(400,180);

inputTimer = new QTimer(this);

inputTimer->setInterval(100);

connect(inputTimer,&QTimer::timeout,this,&Widget::inputValue);

inputTimer->start();

}

Widget::~Widget()

{

if(inputTimer->isActive())

{

inputTimer->stop();

}

delete ui;

}

void Widget::inputValue()

{

if(_isForward)

_currentValue+=1;

else

_currentValue-=1;

if(_currentValue>=_maxValue)

{

_currentValue=_maxValue;

_isForward=false;

}

if(_currentValue<=_minValue)

{

_currentValue=_minValue;

_isForward=true;

}

update();

}

void Widget::paintEvent(QPaintEvent *)

{

QPainter painter(this);

painter.setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing);

drawBorder(&painter);

drawBackground(&painter);

drawText(&painter);

}

void Widget::drawBorder(QPainter *painter)

{

painter->save();

painter->setPen(QPen(Qt::gray,5));

painter->setBrush(Qt::NoBrush);

batteryRect=QRectF((width()-_batteryWidth)/2,(height()-_batteryHeight)/2,_batteryWidth,_batteryHeight);

painter->drawRoundedRect(batteryRect,2,2);

painter->setPen(QPen(Qt::gray,5));

QLineF line(batteryRect.topRight().x()+5,batteryRect.topRight().y()+5,batteryRect.topRight().x()+5,batteryRect.bottomRight().y()-5);

painter->drawLine(line);

painter->restore();

}

void Widget::drawBackground(QPainter *painter)

{

painter->save();

if(_currentValue<=10)

painter->setBrush(QColor(204,38,38));

if(_currentValue<=20)

painter->setBrush(QColor(190,163,0));

else

painter->setBrush(QColor(50,205,51));

double width = _currentValue*(batteryRect.width()-(_margin*2))/100;

QPointF topleft(batteryRect.topLeft().x()+_margin,batteryRect.topLeft().y()+_margin);

QPointF bottomRight(batteryRect.topLeft().x()+_margin+width,batteryRect.bottomRight().y()-_margin);

QRectF rect(topleft,bottomRight);

painter->setPen(Qt::NoPen);

painter->drawRoundRect(rect,5,5);

painter->restore();

}

void Widget::drawText(QPainter *painter)

{

painter->save();

painter->setPen(Qt::black);

painter->setFont(QFont("Arial",11));

QString value=QString::number(_currentValue)+"%";

painter->drawText(batteryRect,Qt::AlignCenter,value);

painter->restore();

}

相关推荐
肥or胖9 小时前
【FFmpeg 快速入门】本地播放器 项目
开发语言·qt·ffmpeg·音视频
小灰灰搞电子15 小时前
Qt Quick 粒子系统详解
开发语言·qt
暴躁茹18 小时前
Qt 将触摸事件转换为鼠标事件(Qt4和Qt5及以上版本)
开发语言·qt·计算机外设
大专生学编程19 小时前
QT简介和QT环境搭建
c++·qt
feiyangqingyun19 小时前
Qt/C++开发监控GB28181系统/视频点播没有ssrc问题的处理/兼容各种设备和应用场景需求
c++·qt·gb28181
小堃学编程21 小时前
QT跨平台应用程序开发框架(10)—— Qt窗口
开发语言·qt
轩宇^_^21 小时前
Qt CMake 学习文档
数据库·qt·学习
小徐不徐说21 小时前
QT技巧之快速搭建串口收发平台
开发语言·c++·qt·串口·软件构建·个人开发·通信
天堂陌客21 小时前
QT 交叉编译环境下,嵌入式设备显示字体大小和QT Creator 桌面显示不一致问题解决
开发语言·qt·字库
小堃学编程1 天前
QT跨平台应用程序开发框架(9)—— 容器类控件
开发语言·qt