您好,登錄后才能下訂單哦!
這篇文章主要介紹如何使用Qt自定義控件實現圓盤進度條,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
具體內容如下
自定義控件二:圓盤進度條
主要思路:使用qpainter根據圖形需求 畫圓和圓弧,畫指針(多邊形,指定坐標即可),根據具體的value值旋轉坐標系,使指針達到旋轉效果,旋轉度數是根據value值,總共360度,占比求得。最中間需要畫文字上去,每次更新value時調用update()方法,重繪界面。
主要代碼:CMPassrate1.cpp
void CMPassrate1::paintEvent(QPaintEvent *event){ int width = this->width(); int height = this->height(); int side = qMin(width, height); QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); painter.translate(width / 2, height / 2); painter.scale(side / 200.0, side / 200.0); //繪制外圓 paintSide(&painter); paintOutE(&painter); paintLine(&painter);//繪制刻度線 paintInE(&painter); paintPoint(&painter);//繪制指針 paintTextE(&painter);//繪制文字所在的圓 paintText(&painter);//繪制文字 paintValue(&painter);//繪制value對應的填充角度}void CMPassrate1::paintSide(QPainter *painter){ int radis = outRadis; QRect rect(-radis,-radis,radis*2,radis*2); painter->save(); painter->setBrush(QBrush(QColor("#505050"))); painter->setPen(Qt::NoPen); painter->drawEllipse(rect); painter->restore();}void CMPassrate1::paintOutE(QPainter *painter){ int radis = outRadis-side; QRect rect(-radis,-radis,radis*2,radis*2); painter->save(); painter->setBrush(QBrush(QColor("#868686"))); painter->setPen(Qt::NoPen); painter->drawEllipse(rect); painter->restore();}void CMPassrate1::paintLine(QPainter *painter){ int lineStart = outRadis-3; painter->save(); painter->setPen(QColor("#868686")); int range = 360/12; for(int i = 0;i<12;i++){ painter->rotate(range); painter->drawLine(QPoint(lineStart,0),QPoint(outRadis,0)); } painter->restore();}void CMPassrate1::paintInE(QPainter *painter){ int radis = inRadis; QRect rect(-radis,-radis,radis*2,radis*2); painter->save(); painter->setBrush(QBrush(QColor("#646464"))); painter->setPen(Qt::NoPen); painter->drawEllipse(rect); painter->restore();}void CMPassrate1::paintTextE(QPainter *painter){ int radis = 23; QRect rect(-radis,-radis,radis*2,radis*2); painter->save(); painter->setBrush(QBrush(QColor("#FFFFFF"))); painter->setPen(Qt::NoPen); painter->drawEllipse(rect); painter->restore();}void CMPassrate1::paintPoint(QPainter *painter){ painter->save(); const QPoint points[3] = { QPoint(10,0), QPoint(-10,0), QPoint(0,inRadis-5) }; int range = ((double)value/100)*360; painter->rotate(range); painter->setPen(Qt::NoPen); painter->setBrush(QBrush(QColor("#66CFFF"))); painter->drawConvexPolygon(points,3); painter->restore();}void CMPassrate1::paintText(QPainter *painter){ int radis = 23; QRect rect(-radis,-radis,radis*2,radis*2); painter->save(); painter->setBrush(QBrush(QColor("#000000"))); painter->setPen(QPen(QColor("#000000"))); QFont font = painter->font(); font.setPixelSize(20); painter->setFont(font); painter->drawText(rect,Qt::AlignCenter,QString("%1%").arg(QString::number(value))); painter->restore();}void CMPassrate1::paintValue(QPainter *painter){ int oRandis = outRadis-side; qDebug()<<"value::"<<value; int rangle = ((double)value/100)*360; QRectF rect(-oRandis, -oRandis, oRandis << 1, oRandis << 1); QPainterPath path; qDebug()<<"rangle:"<<rangle<<" start:"<<270-rangle; path.arcTo(rect, 270-rangle, rangle); // QRectF(-120, -120, 240, 240) QPainterPath subPath; subPath.addEllipse(rect.adjusted((oRandis-inRadis),(oRandis-inRadis),-(oRandis-inRadis),-(oRandis-inRadis))); // path為扇形 subPath為橢圓 path -= subPath; painter->save(); painter->setBrush(valueColor); painter->setPen(Qt::NoPen); painter->drawPath(path); painter->restore();}
以上是“如何使用Qt自定義控件實現圓盤進度條”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。