绘画事件
void Dot::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::RenderHint::Antialiasing, true);
setPenStyle(painter);
setBrushStyle(painter);
}
画笔设置
void Dot::setPenStyle(QPainter &painter)
{
int penWidth = 3;
QColor penColor = QColor(0,0,255);
QPen pen;
pen.setColor(penColor);
pen.setWidth(penWidth);
pen.setStyle(Qt::PenStyle::DashDotDotLine);
pen.setCapStyle(Qt::PenCapStyle::RoundCap);
pen.setJoinStyle(Qt::PenJoinStyle::RoundJoin);
pen.setMiterLimit(3);
painter.setPen(pen);
}
void Dot::setBrushStyle(QPainter &painter)
{
qreal hrefWidth = this->geometry().width()/2;
qreal hrefHeight = this->geometry().height()/2;
qreal radio = qMin(hrefWidth, hrefHeight) - painter.pen().width();
QColor fullColor = QColor(80, 80, 255);
QLinearGradient lineGradient(hrefWidth, hrefHeight- radio, hrefWidth, hrefHeight + radio);
lineGradient.setColorAt(0, QColor(255, 255, 255));
lineGradient.setColorAt(1, fullColor);
QConicalGradient conicalGradient(hrefWidth, hrefHeight, 0);
conicalGradient.setColorAt(0, fullColor);
conicalGradient.setColorAt(1, QColor(255, 255, 255));
QRadialGradient radiaGradient(hrefWidth, hrefHeight, radio, this->geometry().width()/3, this->geometry().height()/3, radio/10);
radiaGradient.setColorAt(0, fullColor);
radiaGradient.setColorAt(1, QColor(255, 255, 255));
QBrush brush(lineGradient);
brush.setStyle(Qt::BrushStyle::LinearGradientPattern);
painter.setBrush(brush);
}
画文本
void Dot::drawText(QPainter &painter)
{
painter.setPen(QPen(QColor(255,255,0)));
painter.setBrush(Qt::BrushStyle::NoBrush);
QFont font = QFont("宋体", 22);
QFontMetricsF metrics(font);
QString str = "30%";
qreal fontWidth = metrics.width(str);
qreal fontHeight = metrics.height();
painter.save();
QTransform trans;
trans.translate(this->geometry().width()/2,this->geometry().height()*0.8);
painter.setTransform(trans);
painter.setFont(font);
painter.drawRect(-(fontWidth/2),-(fontHeight/2), fontWidth, fontHeight);
painter.drawText(-(fontWidth/2),-(fontHeight/2), fontWidth, fontHeight, Qt::AlignCenter, str);
painter.restore();
}