96SEO 2026-08-08 22:39 4
在实际项目中,很多进度展示场景并不适合传统的横向进度条。说起来,从例如来看,
针对这些痛点。这篇文章提供两套基于 Canvas 的环形进度实现:

下面是封装好的 CircleProgress 组件。默认显示百分比,几秒后自动切换到 “加载中” 的旋转模式,再切回普通模式。
import QtQuick 2.15
import QtQuick.Canvas 1.0
Item {
id的观点是,root
// ---------- 公共属性 ----------
property real value: 0 // 0~1
property bool indeterminate: false // 不确定模式开关
property int lineWidth: 12 // 环宽
property color progressColor: "#3498db" // 前景色
property color trackColor: "#e6e6e6" // 背景轨道色
property int animationDuration: 500 // 百分比变化动画时长
implicitWidth: 200
implicitHeight: 200
readonly property real radius: Math.min / 2 - lineWidth / 2
// -------- 正常进度动画 --------
property real animatedValue: value
Behavior on animatedValue {
enabled:!root.indeterminate // 不确定模式下禁用动画
NumberAnimation {
duration: root.animationDuration
easing.type: Easing.OutCubic
}
}
Canvas {
id的观点是,canvas
anchors.fill: parent
antialiasing: true
onPaint: {
var ctx = getContext;ctx.clearRect;var cx = width / 2;怎么说呢,var cy = height / 2;var r = root.radius;// ---- 背景轨道 ----
ctx.beginPath;ctx.arc,ctx.strokeStyle = root.trackColor;按理说,ctx.lineWidth = root.lineWidth;ctx.stroke,// ---- 前景进度弧 ----
var sweep = root.indeterminate?:,ctx.beginPath;ctx.arc(cx,cy,r,-Math.PI / 2。-Math.PI / 2 + sweep);ctx.strokeStyle = root.progressColor;ctx.lineWidth = root.lineWidth;ctx.lineCap = "round";ctx.stroke,}
// 属性变化时请求重绘
Connections {
再看target,root
function onAnimatedValueChanged { canvas.requestPaint;}
function onIndeterminateChanged { canvas.requestPaint;}
function onProgressColorChanged { canvas.requestPaint;}
function onTrackColorChanged { canvas.requestPaint;}
function onLineWidthChanged { canvas.requestPaint;}
}
// 不确定模式:给整个 Canvas 加旋转动画。让固定弧段转起来
RotationAnimation on rotation {
至于from,0;to: 360,duration: 1200;running: root.indeterminate;loops: Animation.Infinite;}
}
// 中心文字:百分比或“加载中”
Text {
anchors.centerIn: parent
至于text,root.indeterminate?"加载中"
: Math.round + "%"
font.pixelSize: root.radius * 0.5
font.bold: true
至于color。root.progressColor
visible: true
}
}
import QtQuick 2.15
import QtQuick.Layouts 1.15
ColumnLayout {
anchors.fill : parent
anchors.margins : 20
spacing : 20
CircleProgress {
id : control
Layout.preferredWidth : parent.width * .4
Layout.preferredHeight : parent.width * .4
Layout.alignment : Qt.AlignHCenter
}
Timer {
id : timerSwitchMode
interval : 3000 // 每三秒切换一次模式,以演示两种状态的切换效果
running : true
repeat : true
onTriggered : control.indeterminate =!control.indeterminate;}
Item { Layout.fillHeight : true } // 占位,让布局居中
}
Shape 在每帧重新栅格化会导致卡顿且边缘出现锯齿;而 Canvas 绘制一次后缓存为纹理。随后只做 GPU 层面的变换,性能更佳且视觉更平滑。onPaint
仅绘制两条弧线:
-π/2),扫过角度依据当前值计算。使用 lineCap="round" 获得圆润的端点。
readonly property real radius:
Math.min / 2 - lineWidth / 2;怎么说呢,
若不减去线宽的一半。粗线会超出组件边界,这是画圆环时最常遗漏的细节。animatedValue 配合 Connections`监听属性变化并调用 canvas.requestPaint` 重绘。使得进度条随数值平滑增长,同时中心文字同步更新。indeterminate=true` 时仅绘制固定长度的弧段,并对整个 Cavas` 添加无限循环的旋转动画。因为背景是完整圆形,即使整体旋转也不会被察觉。这样就能实现“加载中”动效。The three concentric rings represent CPU、Memory、Disk three key metrics. Each ring is driven by a dedicated slider;center shows a composite value.
import QtQuick 2.15
import QtQuick.Controls 2.15
ColumnLayout {
anchors.fill : parent
anchors.margins : 20
spacing : 20
RowLayout {
Layout.fillWidth : true
/* ---------- 圆形仪表盘 ---------- */
Canvas {
id : multiCanvas
Layout.preferredWidth : parent.width * .5
Layout.preferredHeight : parent.width * .5
/* 与三个 Slider 双向绑定的属性 */
property real p1 : sliderCpu.value // CPU
property real p2 : sliderMem.value // Memory
property real p3 : sliderDisk.value // Disk
/* 任意属性变化即请求重绘 */
onP1Changed : requestPaint
onP2Changed : requestPaint
onP3Changed : requestPaint
onPaint:{
var ctx = getContext;ctx.clearRect;var cx = width/2;var cy = height/2;/* 三个环的配置,从外到内依次缩小半径 */
var rings =;for{
var ring = rings;
其实,/* 背景轨道 */
ctx.beginPath;ctx.arc,ctx.strokeStyle=ring.bg;ctx.lineWidth=lineWidth;ctx.stroke,/* 前景进度 */
var sweep = ring.progress*Math.PI*2;ctx.beginPath;ctx.arc,ctx.strokeStyle=ring.color;ctx.lineWidth=lineWidth;ctx.lineCap="round";ctx.stroke,}
/* 中心文字 – 综合值取第二层作为示例 */
var composite = Math.round;ctx.fillStyle="#333333";ctx.font="bold 24px sans-serif";不过,ctx.textAlign="center";ctx.textBaseline="middle";ctx.fillText;/* 小字 “综合” */
ctx.font="12px sans-serif";ctx.fillText;}
}
/* ---------- 控件 & 图例 ---------- */
ColumnLayout{
spacing :10
Layout.alignment :Qt.AlignVCenter
/* CPU */
RowLayout{
spacing :-5
Rectangle{ width:pxToDp;height:pxToDp;radius:pxToDp;color:"#E17055"}
Text{ text:"CPU";话说回来,font.pointSize:=10}
Text{ text:.toFixed+"%";font.pointSize:=10;font.bold:true;color:"#E17055"}
}
Slider{id:sliderCpu;from:.0,to:.99;value:.65,话说回来,Layout.preferredWidth:auto;}
/* Memory */
RowLayout{
spacing:-5
Rectangle{ width:pxToDp;怎么说呢,height:pxToDp;radius:pxToDp;color:"#00B894"}
Text{ text:"内存";font.pointSize:=10}
Text{ text:.toFixed+"%";font.pointSize:=10;font.bold:true;说起来,color:"#00B894"}
}
Slider{id:sliderMem;from:.0,to:.99;value:.45,Layout.preferredWidth:auto;}
/* Disk */
RowLayout{
spacing:-5
Rectangle{ width:pxToDp;height:pxToDp;radius:pxToDp;color:"#6C5CE7"}
Text{ text:"磁盘";font.pointSize:=10}
Text{ text:.toFixed+"%";font.pointSize:=10;font.bold:true;color:"#6C5CE7"}
}
Slider{id:sliderDisk;from:.0,to:.99;value:.30,Layout.preferredWidth:auto;}
}
}
Item{ Layout.fillHeight:true }
}
.requestPaint。保证 UI 与数据同步,无需手动刷新或额外计时器。
| 维度 | 单环进度 | 多环进度 |
|---|---|---|
| 形态 | 单环 + 中心百分比 | 三环同心 + 中心综合值 |
| 数据来源 | 外部属性 value |
三个外部 Slider 联动 |
| 不确定模式 | 有 – 使用旋转动画展示“加载中” | 无 – 始终展示具体数值 |
| 适用场景 | 单指标加载、评分或占位展示 | 多指标仪表盘对比、资源监控等 |
| ⚡️ 根据实际需求挑选合适方案 ⚡️ 💡 如需同时兼顾两者。可把单环组件嵌入多环布局,实现混搭效果 💡 🚀 常用方法:统一使用 Canvas 绘制、属性驱动刷新 ✅ 🛠️ 可 性强,可随业务动态增删圈数 ✅ 🔧 支持主题色与线宽自定义 ✅ ✅ | ||
请忽略以上乱码,它们只是占位符
...
请忽略上方残余字符。
`
此处提供了一个结构化对照表,使您快速判断哪种实现更契合业务需求。
--
- - - - - -
—-
—- —
—
——
——–
—– —– —–
)
• 使用 Canvas 能够让你比较容易做到高性能、低卡顿的圆形或同心多圈动态效果。
• 行为分离将 UI 渲染交给 Canvas。将业务状态放在 QML 属性上,通过 Connections 实现即时刷帧。
• 可 只需修改数组即可支持任意数量的指示圈,实现灵活仪表盘。
————————————————
bash
qmake && make && ./YourApp
作为专业的SEO优化服务提供商,我们致力于通过科学、系统的搜索引擎优化策略,帮助企业在百度、Google等搜索引擎中获得更高的排名和流量。我们的服务涵盖网站结构优化、内容优化、技术SEO和链接建设等多个维度。
| 服务项目 | 基础套餐 | 标准套餐 | 高级定制 |
|---|---|---|---|
| 关键词优化数量 | 10-20个核心词 | 30-50个核心词+长尾词 | 80-150个全方位覆盖 |
| 内容优化 | 基础页面优化 | 全站内容优化+每月5篇原创 | 个性化内容策略+每月15篇原创 |
| 技术SEO | 基本技术检查 | 全面技术优化+移动适配 | 深度技术重构+性能优化 |
| 外链建设 | 每月5-10条 | 每月20-30条高质量外链 | 每月50+条多渠道外链 |
| 数据报告 | 月度基础报告 | 双周详细报告+分析 | 每周深度报告+策略调整 |
| 效果保障 | 3-6个月见效 | 2-4个月见效 | 1-3个月快速见效 |
我们的SEO优化服务遵循科学严谨的流程,确保每一步都基于数据分析和行业最佳实践:
全面检测网站技术问题、内容质量、竞争对手情况,制定个性化优化方案。
基于用户搜索意图和商业目标,制定全面的关键词矩阵和布局策略。
解决网站技术问题,优化网站结构,提升页面速度和移动端体验。
创作高质量原创内容,优化现有页面,建立内容更新机制。
获取高质量外部链接,建立品牌在线影响力,提升网站权威度。
持续监控排名、流量和转化数据,根据效果调整优化策略。
基于我们服务的客户数据统计,平均优化效果如下:
我们坚信,真正的SEO优化不仅仅是追求排名,而是通过提供优质内容、优化用户体验、建立网站权威,最终实现可持续的业务增长。我们的目标是与客户建立长期合作关系,共同成长。
Demand feedback