這篇文章主要介紹如何使用Qt自定義控件實現(xiàn)圓盤進度條,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
在良慶等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供做網(wǎng)站、成都網(wǎng)站制作 網(wǎng)站設(shè)計制作按需定制,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都營銷網(wǎng)站建設(shè),外貿(mào)營銷網(wǎng)站建設(shè),良慶網(wǎng)站建設(shè)費用合理。具體內(nèi)容如下
自定義控件二:圓盤進度條
主要思路:使用qpainter根據(jù)圖形需求 畫圓和圓弧,畫指針(多邊形,指定坐標(biāo)即可),根據(jù)具體的value值旋轉(zhuǎn)坐標(biāo)系,使指針達到旋轉(zhuǎn)效果,旋轉(zhuǎn)度數(shù)是根據(jù)value值,總共360度,占比求得。最中間需要畫文字上去,每次更新value時調(diào)用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對應(yīng)的填充角度}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自定義控件實現(xiàn)圓盤進度條”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站欄目:如何使用Qt自定義控件實現(xiàn)圓盤進度條-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://jinyejixie.com/article22/dhogcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、做網(wǎng)站、Google、外貿(mào)建站、靜態(tài)網(wǎng)站、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容