Qt QML 基于平行四边形的进度条(Matrix4x4、drag)

Qt QML 基于平行四边形的进度条


所有的热爱都要不遗余力,真正喜欢它便给它更高的优先级,和更多的时间吧!

GIT工程文件在这里:     QmlLearningPro
QML其它文章请点击这里:     QT QUICK QML 学习笔记


1. 演示

    废话不多,上图:
在这里插入图片描述

2. 实现过程

1. 先实现平行四边形: 通过 Rectangle 的矩阵变化来实现 (transform: Matrix4x4):

Rectangle {
    id: rect
    ...
    // 切变矩阵
    transform: Matrix4x4 {
        matrix: Qt.matrix4x4(1, xs, 0, 0,   // xs平方向切变     +表示朝右 
                             ys, 1, 0, 0,   // ys垂直方向切变   +表示朝下 
                             0, 0, 1, 0,
                             0, 0, 0, 1)

    }
}

参考:https://blog.csdn.net/weixin_39644614/article/details/112220330

2. 再实现 15个平行四边形:通过 Row 结合 Repeater 即可:

Row {
    id:  row
    ...
    Repeater {
        id: rep
        model: 15
        Parallelogram {
            xs:  -0.6
            ys:  0.01
            ...
        }
    }
}

3. 通过两种不同颜色的平行四边形来实现进度条的百分比:创建一个显示方块的个数的属性来控制

property int showIndex: 0  
onShowIndexChanged: {
    for(var i=0; i<15; i++) {
        if (row.children[i].toString().startsWith("Parallelogram")) {
            if(i<showIndex) 	row.children[i].color = "lightblue"
 			else 				row.children[i].color = "white"
        }
    }
}

4. 创建一个滑动进度条提供输入量:

Rectangle {
        id: container
        ...
        //小滑块条
        Rectangle {
            id: slider
            ...
            MouseArea {
               ...
                drag.axis: Drag.XAxis
            }
            //滑块变动,影响 showIndex 的变化
            onXChanged:{
                showIndex = 15*x/(sliderMax)
            }
        }
    }

5. 同时也创建了两个按键提供输入量:

Button {
    id: sub
    text: "SUB"
    ...
    onClicked:  {
        showIndex--
        if(showIndex <= 0) showIndex=0;
        slider.x = showIndex * sliderMax / 15
    }
}
Button {
    text: "ADD"
    ...
    onClicked: {
        showIndex++
        if(showIndex >= 15) showIndex=15;
        slider.x = showIndex * sliderMax / 15
    }
}

3. 完整代码

Parallelogram.qml:

import QtQuick 2.12

Item {
    property real xs: 0                 // 水平方向切变
    property real ys: 0                 // 垂直方向切变
    property alias radius: rect.radius  // 圆角
    property alias text: title.text     // 文本
    property alias color: rect.color

    Rectangle {
        id: rect
        anchors.fill: parent
        color: "lightblue"
        // 切变矩阵
        transform: Matrix4x4 {
            matrix: Qt.matrix4x4(1, xs, 0, 0,
                                 ys, 1, 0, 0,
                                 0, 0, 1, 0,
                                 0, 0, 0, 1)

        }

        Text {
            id: title
            anchors.centerIn: rect
            text: "0"
        }
    }
}

main.qml:

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5

Window {
    visible: true

    width: 600
    height: 220
    title: qsTr("不一样的进度条!")
    color: "#404040"

    property int showIndex: 0  //lightblue
    property int _pix:      20
    property int sliderMax: container.width - slider.width-1

    onShowIndexChanged: {
        for(var i=0; i<15; i++) {
            if (row.children[i].toString().startsWith("Parallelogram")) {
                if(i<showIndex) {
                    row.children[i].color = "lightblue"
                }
                else {
                    row.children[i].color = "white"
                }
            }
        }
    }

    Row {
        id:  row
        anchors.horizontalCenter: parent.horizontalCenter
        y: 40
        spacing: 4

        Repeater {
            id: rep
            model: 15

            Parallelogram {
                xs:  -0.6
                ys:  0.01
                radius: 3
                width: 28
                color:    "white"
                height: 10 + (28-10) * (15-index) / 15
                anchors.bottom: parent.bottom
//                anchors.bottomMargin: -index
                text: index + 1
                }
        }
    }

    Rectangle {
        id: container
        anchors {
            top: row.bottom
            topMargin: 30
            left: row.left
            leftMargin: -10
        }
        width:                  row.width
        height:                 _pix
        radius:                 height/2;
        opacity: 0.6                                //不透明度
        antialiasing: true                          // 抗锯齿,具体效果见下面图片
        //渐变色
        gradient: Gradient {
            GradientStop { position: 0.0; color: "White" }
            GradientStop { position: 1.0; color: "LightSalmon" }
        }

        //小滑块条
        Rectangle {
            id: slider
            x: 0
            y: 2
            width:              _pix*2;
            height:             _pix-4
            radius:             height/2;
            antialiasing: true
            gradient: Gradient {
                GradientStop { position: 0.0; color: "green" }
                GradientStop { position: 1.0; color: "aqua" }
            }
            MouseArea {
                anchors.fill: parent
                anchors.margins: -_pix
                drag.target: parent;
                drag.axis: Drag.XAxis
                drag.minimumX: 1;
                drag.maximumX: sliderMax;
            }
            //滑块变动,影响 showIndex 的变化
            onXChanged:{
                showIndex = 15*x/(sliderMax)
            }
        }
    }

    Button {
        id: sub
        text: "SUB"
        anchors.top:    container.bottom
        anchors.topMargin: 20
        anchors.left:   container.left

        onClicked:  {
            showIndex--
            if(showIndex <= 0) showIndex=0;
            slider.x = showIndex * sliderMax / 15
        }
    }
    Button {
        text: "ADD"
        anchors.top:    container.bottom
        anchors.topMargin: 20
        anchors.left:   sub.right
        anchors.leftMargin: 20

        onClicked: {
            showIndex++
            if(showIndex >= 15) showIndex=15;
            slider.x = showIndex * sliderMax / 15
        }
    }
}

GIT 工程文件点击这里:     QmlLearningPro
QML 其它文章请点击这里:     QT QUICK QML 学习笔记

  • 11
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
Qt QML 中的进度条可以通过 QML 的 ProgressBar 组件来实现。进度条用于显示任务的进度,通常用于长时间运行的任务中,让用户了解任务的完成情况。 在 QML 中使用进度条,首先需要导入 QtQuick 控件库: ``` import QtQuick.Controls 2.12 ``` 然后可以在界面中加入一个 ProgressBar 组件: ``` ProgressBar { minimumValue: 0 // 进度条的最小值 maximumValue: 100 // 进度条的最大值 value: 50 // 当前的进度值 } ``` 在上面的例子中,我们设置了进度条的最小值为 0,最大值为 100,当前进度值为 50。 进度条还可以通过设置不同的属性来自定义样式,例如可以设置颜色、高度、方向等: ``` ProgressBar { width: 200 height: 20 progressColor: "blue" orientation: ProgressBar.Horizontal } ``` 在上面的例子中,我们设置了进度条的宽度为 200 像素,高度为 20 像素,进度条的颜色为蓝色,进度条的方向为水平方向。 进度条还可以通过绑定属性来动态更新进度值。例如,可以将进度条的值与后台任务的进度相关联,实时更新进度条的显示: ``` ProgressBar { id: progressBar value: backendTask.progress } SomeBackendTask { id: backendTask onProgressChanged: { progressBar.value = progress } } ``` 在上面的例子中,我们将进度条的值绑定到后台任务的 progress 属性,当后台任务的进度改变时,进度条的值也会相应地改变。 Qt QML 提供了丰富的功能和样式选项,可以根据实际需求进行进一步的定制和扩展。以上是关于如何在 Qt QML 中使用进度条的简单介绍。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值