qt 自定义控件
void MyProgreessBar::paintEvent(QPaintEvent* event){ QPainter p(this); QRect rc = rect(); p.setRenderHint(QPainter::Antialiasing); int barWidget = 20; int circleX = rc.width() / 2 ; int circleY = rc.height() / 2; int m_persent = 20; int m_rotateAngle = 360 * m_persent / 100; int allAngle = 360 * 100 / 100; int side = qMin(rc.width(), rc.height()); QRectF outRect(0, 0, side, side); QRectF inRect(barWidget, barWidget, side - 40, side - 40); QString valueStr = QString("%1%").arg(QString::number(m_persent)); //画外圆 //p.setPen(Qt::NoPen); //p.setBrush(QBrush(QColor(97, 117, 118))); //p.drawEllipse(outRect); //p.setBrush(QBrush(QColor(255, 107, 107))); //p.drawPie(outRect, (90 - m_rotateAngle) * 16, m_rotateAngle * 16); //绘制圆弧背景 QPen pen; pen.setCapStyle(Qt::RoundCap); pen.setWidthF(20); pen.setColor(QColor(97, 117, 118)); p.setPen(pen); p.drawArc(inRect, 360 * 16, 360 * 16); //绘制圆弧进度 pen.setColor(QColor(255, 107, 107)); p.setPen(pen); p.drawArc(inRect, 0 * 16, m_rotateAngle * 16); //画文字 QFont f = QFont("Microsoft YaHei", 15, QFont::Bold); p.setFont(f); p.setFont(f); p.setPen(QColor("#555555")); p.drawText(inRect, Qt::AlignCenter, valueStr);}
评论