void PiePaintWdt::SetData(qreal algle)
{
m_algle = algle;
this->repaint();
}
QPointF PiePaintWdt::getQPointFDelta(QFont PaintFont, qreal X, qreal Y, qreal deltaX, qreal deltaY,qreal algle)
{
QPointF delta(0.0, 0.0);
if ((algle >= 0 && algle < 180)) {
delta = QPoint(deltaX + X + getXDelta(algle), Y - deltaY - getYDelta(algle) - 3);
} else if ((algle >= 180 && algle < 360)) {
delta = QPoint(deltaX + X + getHXDelta(PaintFont, algle, m_strUnuse), Y - deltaY - getYDelta(algle) - 3);
} else {
delta = QPoint(deltaX + X + getXDelta(algle), Y - deltaY - getYDelta(algle) - 3);
}
if ((algle >= 0 && algle < 180)) {
delta += QPoint(+ 10, 0);
} else if ((algle >= 180 && algle < 360)) {
delta = delta += QPoint(+ 10, 0);
} else{
delta += QPoint(+10, 0);
}
return delta;
}
qreal PiePaintWdt::getXDelta(qreal algle)
{
qreal delta = 10;
if ((algle >= 0 && algle < 180)) {
delta = delta;
}
else if ((algle >= 180 && algle < 360)) {
delta = -delta;
} else {
delta = delta;
}
return delta;
}
qreal PiePaintWdt::getHXDelta(QFont PaintFont, qreal algle, QString strText)
{
qreal delta = QFontMetrics(PaintFont).width(strText) + 20;
if ((algle >= 0 && algle < 180)) {
delta = delta;
}
else if ((algle >= 180 && algle < 360)) {
delta = -delta;
}
else {
delta = delta;
}
return delta;
}
qreal PiePaintWdt::getYDelta(qreal algle)
{
qreal delta = 20;
if ((algle >= 0 && algle < 90)) {
delta = delta;
}
else if((algle >= 90 && algle < 180)){
delta = -delta;
}
else if ((algle >= 180 && algle < 270)) {
delta = -delta;
}
else {
delta = delta;
}
return delta;
}
void PiePaintWdt::paintEvent(QPaintEvent *event)
{
QPainter m_painter = QPainter(this);
// text
QFont font("Microsoft YaHei UI", 8);
m_painter.setPen(Qt::NoPen);
m_painter.setFont(font);
//设置反锯齿
m_painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::Qt4CompatiblePainting);
// 环
QRect drawRect = this->rect();
int x = drawRect.width() / 2;
int y = drawRect.height() / 2;
float radians = qDegreesToRadians(45.0);
qreal r1 = drawRect.height() / 3.0;
qreal r2 = drawRect.height() / 3.5;
qreal r3 = drawRect.height() / 8.0;
qreal h1 = r1 * qSin(radians);
qreal h2 = r2 * qSin(radians);
qreal h3 = r3 * qSin(radians);
m_painter.setBrush(QColor(61, 113, 250));
m_painter.drawPie(x - h1, y - h1, h1 * 2, h1 * 2, - (270 * 16 + m_algle * 16), - (360 - m_algle) * 16);
m_painter.setBrush(QColor(252, 169, 45));// 已使用 黄色
m_painter.drawPie(x - h2, y - h2, h2 * 2, h2 * 2, - (0 + 270 * 16), - m_algle * 16); // 已使用
// 中心的洞
m_painter.setBrush(m_painter.background());
m_painter.drawEllipse(QPointF(x, y), r3, r3);
// 已使用 arm
qreal arm = r2 - 5;
qreal deltaX1 = arm * qSin(qDegreesToRadians(m_algle / 2));
qreal deltaY1 = arm * qCos(qDegreesToRadians(m_algle / 2));
qreal deltaX2 = arm * qSin(qDegreesToRadians(m_algle / 2));
qreal deltaY2 = arm * qCos(qDegreesToRadians(m_algle / 2));
m_painter.setPen(QColor(252, 169, 45));
qint64 arg = (qint64)(360 - m_algle) * 100.0 / 360.0;
qint64 arg1 = 100 - arg;
m_struse = QString(tr("Used: ")) + QString("%1").arg(arg1) + QString("%");
m_painter.drawLine(QLine(QPoint(x + deltaX1, y - deltaY1), QPoint(x + deltaX1 + getXDelta(m_algle / 2), y - deltaY1 - getYDelta(m_algle / 2))));
m_painter.drawLine(QLine(QPoint(deltaX1 + x + getXDelta(m_algle / 2), y - deltaY1 - getYDelta(m_algle / 2)), QPoint(deltaX1 + x + getHXDelta(font, m_algle / 2, m_struse), y - deltaY1 - getYDelta(m_algle / 2))));
m_painter.drawText(getQPointFDelta(font, x, y, deltaX1, deltaY1, m_algle / 2), m_struse);
// 未使用 arm
deltaX1 = arm * qSin(qDegreesToRadians(180 + m_algle / 2));
deltaY1 = arm * qCos(qDegreesToRadians(180 + m_algle / 2));
deltaX2 = arm * qSin(qDegreesToRadians(180 + m_algle / 2));
deltaY2 = arm * qCos(qDegreesToRadians(180 + m_algle / 2));
m_painter.setPen(QColor(61, 113, 250));
m_strUnuse = QString(tr("UnUsed: ")) + QString("%1").arg(arg) + QString("%");
m_painter.drawLine(QLine(QPoint(x + deltaX1, y - deltaY1), QPoint(x + deltaX1 + getXDelta((180 + m_algle / 2)), y - deltaY1 - getYDelta((180 + m_algle / 2)))));
m_painter.drawLine(QLine(QPoint(deltaX1 + x + getXDelta((180 + m_algle / 2)), y - deltaY1 - getYDelta((180 + m_algle / 2))), QPoint(deltaX1 + x + getHXDelta(font, (180 + m_algle / 2), m_struse), y - deltaY1 - getYDelta((180 + m_algle / 2)))));
m_painter.drawText(getQPointFDelta(font, x, y, deltaX1, deltaY1, (180 + m_algle / 2)), m_strUnuse);
event->accept();
m_painter.restore();
}
03-04
568
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)