資源簡介
模擬電量進度條
模擬電量進度條

代碼片段和文件信息
#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
評論
共有 條評論