-
UID:176218
-
- 注册时间2016-10-15
- 最后登录2019-12-19
- 在线时间19小时
-
- 发帖52
- 搜Ta的帖子
- 精华0
- 金钱577
- 威望64
- 贡献值0
- 好评度54
-
访问TA的空间加好友用道具
|
简介 本文是《Qml组件化编程》系列文章的第六篇,涛哥将教大家,进度条组件的定制。 顺便说一下,涛哥的TaoQuick项目正式开源了, 系列文章中的所有功能,包括动态换皮肤、切换多语言等等,都集成在了TaoQuick中,同时涛哥也在TaoQuick中使用了持续集成(CI)技术,目前已经能够自动编译、发布Windows和 Macos平台的软件包,可以在github的Release界面下载体验。互联网行业很流行的DevOps理念,在TaoQuick项目中得到了最佳的实践。(linux平台的发布工具linuxdeployqt暂时还有点问题,涛哥后续会搞定的)地址在这https://github.com/jaredtao/TaoQuick, 赶快去star吧。 注:文章主要发布在涛哥的博客 和 知乎专栏-涛哥的Qt进阶之路 先看预览图 新的渐变效果 Qt 5.12 加入了新的渐变效果,一共180种,效果来自这个网站https://webgradients.com按照帮助文档的介绍,可以通过下面这两种方式使用- Rectangle {
- y: 0; width: 80; height: 80
- gradient: Gradient.NightFade
- }
- Rectangle {
- y: 0; width: 80; height: 80
- gradient: "NightFade"
- }
涛哥立即想到了,枚举不就是数字嘛- Rectangle {
- y: 0; width: 80; height: 80
- gradient: 1
- }
- Rectangle {
- y: 0; width: 80; height: 80
- gradient: 2
- }
- Rectangle {
- y: 0; width: 80; height: 80
- gradient: 3
- }
试了一下,这样也是可以啊,哈哈。于是涛哥就把180种渐变效果都拉出来看看。 Qt只支持水平和垂直的渐变,其中有小部分是不能用的,所以只有165个能用。看一下展示全部渐变的Qml代码:- import QtQuick 2.9
- import QtQuick.Controls 2.5
- Item {
- anchors.fill: parent
- GridView {
- id: g
- anchors.fill: parent
- anchors.margins: 20
- cellWidth: 160
- cellHeight: 160
- model: 180 //这里的数据Model直接给个数字180
- clip: true
- property var invalidList: [27, 39, 40, 45, 71, 74, 105, 111, 119, 130, 135, 141] //这几个是不能用的,看过运行报错后手动列出来的。
- delegate: Item{
- width: 160
- height: 160
- Rectangle{
- width: 150
- height: 150
- anchors.centerIn: parent
- color: "white"
- radius: 10
- Text {
- anchors.horizontalCenter: parent.horizontalCenter
- anchors.top: parent.top
- anchors.topMargin: 2
- text: index + 1
- }
- Rectangle {
- width: 100
- height: width
- radius: width / 2
- //编号在列表里的,直接渐变赋值为null,就不会在Qml运行时报警告了
- gradient: g.invalidList.indexOf(modelData + 1) < 0 ? modelData + 1 : null
- anchors.centerIn: parent
- anchors.verticalCenterOffset: 10
- }
- }
- }
- }
- }
条形进度条 普通进度条的原理,就是有一个比较长的矩形做背景,在上面放一个颜色不同的矩形,其宽度跟着百分比变化,100%时宽度与背景一致。可以写一个很简要的进度条。- Rectangle {
- id: back
- width: 300
- height: 50
- radius: height / 2
- color: "white"
- property int percent: 0
- Rectangle {
- id: front
- //宽度是 背景宽度 * 百分比
- width: percent / 100 * parent.width
- height: parent.height
- radius: parent.radius
- color: "red"
- }
- }
再添加一点元素,在右侧放一个文本,表示百分比,或者放图片。甚至给进度条加个闪光特效。经过一系列的加工,封装成一个综合的组件,最终结果如下:圆形进度条 将一个Rectangle做成圆形: 宽高相等,半径为宽度一半。再把 颜色设置为透明,边框不透明,边框加粗一点,就是一个圆环了。- Rectangle {
- id: back
- width: 120
- height: width
- radius: width / 2
- color: "transparent"
- border.width: 10
- border.color: "white"
- }
接下来给圆环贴上一个圆形渐变色,渐变按照百分比来做。- import QtGraphicalEffects 1.12
- Rectangle {
- id: back
- width: 120
- height: width
- radius: width / 2
- color: "transparent"
- border.width: 10
- border.color: "white"
- property int percent: 0
- ConicalGradient {
- anchors.fill: back
- source: back
- gradient: Gradient {
- GradientStop { position: 0.0; color: "white" }
- GradientStop { position: percent / 100 ; color: "red" }
- GradientStop { position: percent / 100 + 0.001; color: "white" }
- GradientStop { position: 1.0; color: "white" }
- }
- }
- }
渐变从0 到 percent处都是有渐变颜色的, 再从percent + 0.001 到1.0处,都是背景色,这样就是一个简易的圆形进度条了。不过这里percent为100的情况,圆形渐变处理不了,我们可以特殊处理,直接让背景圆环变成前景色就行了。(既然都100%了,背景肯定是全部被遮住了,那就让背景做前景,藏掉真正的前景)- import QtGraphicalEffects 1.12
- Rectangle {
- id: back
- width: 120
- height: width
- radius: width / 2
- color: "transparent"
- border.width: 10
- border.color: percent === 100 ? "red" : "white" //百分比为100时显示为前景,否则显示为背景
- property int percent: 0
- ConicalGradient {
- anchors.fill: back
- source: back
- enabled: percent != 100 //百分比不为100时有效
- visible: enabled //百分比不为100时有效
- gradient: Gradient {
- GradientStop { position: 0.0; color: "white" }
- GradientStop { position: percent / 100 ; color: "red" }
- GradientStop { position: percent / 100 + 0.001; color: "white" }
- GradientStop { position: 1.0; color: "white" }
- }
- }
- }
再加点料,封装成组件- //CircleProgressBar.qml
- import QtQuick 2.12
- import QtQuick.Controls 2.12
- import QtGraphicalEffects 1.12
- Item {
- id: r
- property int percent: 0
- enum BarType {
- Text,
- SucceedOrFailed,
- NoBar
- }
- readonly property color __backColor: "#f5f5f5"
- readonly property color __blueColor: "#1890ff"
- readonly property color __succeedColor: "#52c41a"
- readonly property color __failedColor: "#f5222d"
- property color backgroundColor: __backColor
- property color frontColor: {
- switch (barType) {
- case TNormalProgress.BarType.SucceedOrFailed:
- return percent === 100 ? __succeedColor : __failedColor
- default:
- return __blueColor
- }
- }
- property string text: String("%1%").arg(percent)
- property var barType: TNormalProgress.BarType.Text
- Rectangle {
- id: back
- color: "transparent"
- anchors.fill: parent
- border.color: percent === 100 ? frontColor : backgroundColor
- border.width: 10
- radius: width / 2
- }
- Text {
- id: t
- enabled: barType === TNormalProgress.BarType.Text
- visible: enabled
- text: r.text
- anchors.centerIn: parent
- verticalAlignment: Text.AlignVCenter
- horizontalAlignment: Text.AlignHCenter
- }
- Image {
- id: image
- source: percent === 100 ? "qrc:/Core/Image/ProgressBar/ok.png" : "qrc:/Core/Image/ProgressBar/fail.png"
- enabled: barType === TNormalProgress.BarType.SucceedOrFailed
- visible: enabled
- scale: 2
- anchors.centerIn: parent
- }
- ConicalGradient {
- anchors.fill: back
- source: back
- enabled: percent != 100
- visible: enabled
- smooth: true
- antialiasing: true
- gradient: Gradient {
- GradientStop { position: 0.0; color: frontColor }
- GradientStop { position: percent / 100 ; color: frontColor }
- GradientStop { position: percent / 100 + 0.001; color: backgroundColor }
- GradientStop { position: 1.0; color: backgroundColor }
- }
- }
- }
最后来个合影 
|