//画电池内部充电图标
//画电池内部充电图标
void BatteryForm::drawCharge(QPainter *painter)
{
painter->save();
painter->setPen(QPen(Qt::red, 1, Qt::SolidLine));
painter->setBrush(Qt::red);
//定义6个点
static const QPointF points[ 6 ] = {
QPointF(batteryRect.width() * 60 / 100 + 13, batteryRect.height() * 15 / 100),//point1
QPointF(batteryRect.width() * 40 / 100 + 13, batteryRect.height() * 55 / 100),//point2
QPointF(batteryRect.width() * 50 / 100 + 13, batteryRect.height() * 55 / 100),//point3
QPointF(batteryRect.width() * 40 / 100 + 13, batteryRect.height() * 90 / 100),//point4
QPointF(batteryRect.width() * 60 / 100 + 13, batteryRect.height() * 45 / 100),//point5
QPointF(batteryRect.width() * 50 / 100 + 13, batteryRect.height() * 45 / 100),//point6
};
painter->drawPolygon(points, 6);
painter->restore();
}
//电池内部显示百分比
void BatteryForm::drawPercentText(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) + '%';
//QRect textRect = textSize.boundingRect(powStr); //得到字符串的rect
painter->setFont(textFont);
painter->setPen(textColor);
/*painter->drawText(batteryRect.width() / 2 - textRect.width() / 2,
batteryRect.height() / 2 + textRect.height(),
powStr);*/
painter->drawText(batteryRect, powStr, Qt::AlignLeft | Qt::AlignVCenter);
painter->restore();
}
这段代码展示了如何使用Qt库在电池图形中绘制充电图标和内部的百分比显示。`drawCharge`方法通过定义六个点来绘制充电形状,而`drawPercentText`方法用于在电池图形内显示当前的电量百分比。这两个方法都是为了实现一个可视化电池状态的用户界面元素。
728

被折叠的 条评论
为什么被折叠?



