您好,登錄后才能下訂單哦!
這篇文章主要介紹了如何使用Qt自定義控件實(shí)現(xiàn)圓圈加載進(jìn)度條,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
思路:畫(huà)一個(gè)占270度的圓弧,然后定義一個(gè)定時(shí)器,定時(shí)旋轉(zhuǎn)坐標(biāo)系,實(shí)現(xiàn)旋轉(zhuǎn)的效果。圓弧需要使用漸變色,實(shí)現(xiàn)顏色越來(lái)越淺的效果
關(guān)鍵代碼:CMProcessBar1.cpp
CMProcessBar1::CMProcessBar1(QWidget *parent) : QWidget(parent), ui(new Ui::CMProcessBar1){ ui->setupUi(this); QTimer *timer = new QTimer; connect(timer,QTimer::timeout,this,updaterRotation);// 定時(shí)旋轉(zhuǎn)坐標(biāo)系 timer->start(3);//定時(shí)3毫秒}CMProcessBar1::~CMProcessBar1(){ delete ui;}void CMProcessBar1::updaterRotation(){ //循環(huán)360度旋轉(zhuǎn)坐標(biāo)系 rotation++; if(rotation == 360){ rotation = 0; } update();}void CMProcessBar1::paintEvent(QPaintEvent *event){//根據(jù)QPaintPath畫(huà)出漸變色的圓弧 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); QConicalGradient gra(QPoint(0,0),0); gra.setColorAt(0,QColor("#3BB6FE")); gra.setColorAt(1,QColor("#FFFFFF")); QBrush brush(gra); int radis = 40; int sider = 5; QRect rect(-radis,-radis,radis*2,radis*2); QPainterPath path; path.arcTo(rect,0, 270); QPainterPath subPath; subPath.addEllipse(rect.adjusted(sider, sider, -sider, -sider)); path = path-subPath; painter.setBrush(brush);//QColor("#66CFFF") painter.setPen(Qt::NoPen); painter.rotate(rotation); painter.drawPath(path);}
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“如何使用Qt自定義控件實(shí)現(xiàn)圓圈加載進(jìn)度條”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。