網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
一、項(xiàng)目介紹
本文介紹利用QPainter實(shí)現(xiàn)自定義圓形進(jìn)度條。
二、項(xiàng)目基本配置
新建一個(gè)Qt案例,項(xiàng)目名稱(chēng)為“RoundprogressbarTest”,基類(lèi)選擇“QWidget”,點(diǎn)擊選中創(chuàng)建UI界面復(fù)選框,完成項(xiàng)目創(chuàng)建。
三、UI界面設(shè)置
UI界面如下:
為簡(jiǎn)單起見(jiàn),這里只設(shè)計(jì)兩個(gè)控件:
序號(hào) | 名稱(chēng) | 類(lèi)型 | 屬性 |
---|---|---|---|
① | pushButton | QPushButton | text:Start |
② | gridLayout | QGridLayout | / |
四、主程序?qū)崿F(xiàn)
4.1 roundprogressbar.h和roundprogressbar.cpp
由于roundprogressbar.h和roundprogressbar.cpp代碼量較大,這里不進(jìn)行展示,僅作簡(jiǎn)要說(shuō)明。
函數(shù)如下:
//設(shè)置初始角度,順時(shí)針逆時(shí)針
void setdefault(int,bool);
//設(shè)置外圈寬度
void setOutterBarWidth(float);
//設(shè)置內(nèi)圈寬度
void setInnerBarWidth(float);
//設(shè)置范圍
void setRange(float, float);
//設(shè)置當(dāng)前值
void setValue(float);
//設(shè)置外圈顏色
void setOutterColor(const QColor&);
//設(shè)置內(nèi)圈漸變色
void setInnerColor(const QColor&,const QColor&);
void setInnerColor(const QColor&);
//設(shè)置默認(rèn)文字顏色
void setDefaultTextColor(const QColor&);
//設(shè)置控制命令
void setControlFlags(int);
//設(shè)置顯示數(shù)字精度
void setPrecision(int);
在構(gòu)造函數(shù)中進(jìn)行了如下初始化設(shè)定:
//設(shè)置初始角度,順時(shí)針逆時(shí)針
setdefault(90,true);
//設(shè)置默認(rèn)外圈寬度
setOutterBarWidth(18);
//設(shè)置默認(rèn)內(nèi)圈寬度
setInnerBarWidth(16);
//設(shè)置默認(rèn)范圍
setRange(0,100);
//設(shè)置默認(rèn)值
setValue(75);
//設(shè)置外圈顏色
setOutterColor(QColor(233,248,248));
//設(shè)置默認(rèn)漸變色
setInnerColor(QColor(49, 177, 190),QColor(133, 243, 244));
//設(shè)置默認(rèn)文字顏色
setDefaultTextColor(QColor(49,177,190));
//設(shè)置默認(rèn)精度
setPrecision(0);
//設(shè)置內(nèi)圈默認(rèn)文字樣式
setInnerDefaultTextStyle(RoundProgressBar::percent);
設(shè)置初始化角度為90度,順時(shí)針,設(shè)置外圈寬度為18,內(nèi)圈寬度為18;設(shè)置默認(rèn)范圍為0-100,設(shè)置默認(rèn)值為75,設(shè)置外圈顏色、漸變色、文本顏色和默認(rèn)精度為0(無(wú)小數(shù))設(shè)置內(nèi)圈文字樣式為percent(百分比樣式)。
4.2 widget.h頭文件
頭文件中引入roundprogressbar.h頭文件,按鈕點(diǎn)擊槽函數(shù)和定時(shí)器對(duì)應(yīng)的槽函數(shù)、timer對(duì)象和bar1對(duì)象:
private slots:
void setText();
void on_pushButton_clicked();
private:
RoundProgressBar* bar1;
QTimer timer;
int i=0;
4.3 widget.cpp源文件
源文件中在構(gòu)造函數(shù)中定義圓形進(jìn)度條和定時(shí)器,將定時(shí)器timeout信號(hào)和槽函數(shù)setText連接:
//*********************** RoundProgressBar ************************
bar1=new RoundProgressBar(this);
bar1->setOutterBarWidth(20);
bar1->setInnerBarWidth(20);
bar1->setValue(0);//設(shè)置默認(rèn)值為0
bar1->setControlFlags(RoundProgressBar::all);
ui->gridLayout->addWidget(bar1,0,0);
//計(jì)時(shí)
timer.setInterval(100);//設(shè)置計(jì)時(shí)間隔為0.1s
connect(&timer,&QTimer::timeout,this,&Widget::setText);
在析構(gòu)函數(shù)中停止定時(shí)器:
Widget::~Widget()
{
if(timer.isActive())
timer.stop();
delete ui;
}
兩個(gè)槽函數(shù)定義如下:
//點(diǎn)擊
void Widget::on_pushButton_clicked()
{
timer.start();
}
void Widget::setText()
{
bar1->setValue(i++);
bar1->repaint();
if(i>100) //100停止
{
timer.stop();
}
}
五、效果演示
完整效果如下:
原文鏈接:https://wendy.blog.csdn.net/article/details/124378255
相關(guān)推薦
- 2022-05-05 docker中通過(guò)nginx+confd動(dòng)態(tài)生成配置的解決方案_docker
- 2022-10-01 iOS簡(jiǎn)單實(shí)現(xiàn)輪播圖效果_IOS
- 2022-03-19 AJAX請(qǐng)求數(shù)據(jù)及實(shí)現(xiàn)跨域的三種方法詳解_AJAX相關(guān)
- 2022-08-16 git中cherry-pick命令的使用教程_其它綜合
- 2022-04-28 教你如何在Centos8-stream安裝PostgreSQL13_PostgreSQL
- 2022-12-11 React高級(jí)特性Context萬(wàn)字詳細(xì)解讀_React
- 2022-07-11 Android?Studio實(shí)現(xiàn)注冊(cè)頁(yè)面跳轉(zhuǎn)登錄頁(yè)面的創(chuàng)建_Android
- 2022-03-29 sklearn中make_blobs的用法詳情_(kāi)python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支