qml自定义滑块

供参考

样式如图

            // 播放速度
            Text {
                text: qsTr("播放速度")
            }

            property real sliderwidth: 300.0

            Slider {
                id: customSlider
                width: 300
                height: 30
                Layout.alignment: Qt.AlignVCenter
                from: 0
                to: 8 // 因为有9个刻度,从0开始
                stepSize: 1
                value: 0

                background: Rectangle {
                    implicitWidth: 300
                    implicitHeight: 30
                    color: "transparent"
                    radius: 15

                    Canvas {
                        implicitWidth: 300
                        implicitHeight: 30
                        // anchors.fill: parent
                        onPaint: {
                            var ctx = getContext("2d");
                            ctx.reset();
                            ctx.lineWidth = 2;
                            ctx.strokeStyle = "#E0E1E4";

                            var tickWidth = 280 / 8;
                            for (var i = 0; i <= 8; i++) {
                                if (i === 0 || i === 3 || i === 6 || i === 8) { // 特别标记的刻度
                                    var label = "";
                                    switch (i) {
                                    case 3: label = "1x"; break;
                                    case 6: label = "10x"; break;
                                    case 8: label = "40x"; break;
                                    case 0: label = "  0.1x"; break;
                                    }
                                    var textX = i * tickWidth - 5; // 调整文本位置
                                    if (label) {
                                        ctx.save();
                                        ctx.font = "12px Arial";
                                        ctx.fillStyle = "#000000";
                                        ctx.fillText(label, textX, 25);
                                        ctx.restore();
                                    }
                                }
                                ctx.beginPath();
                                ctx.moveTo(i * tickWidth, 2);
                                ctx.lineTo(i * tickWidth, 14);
                                ctx.stroke();
                            }

                            ctx.beginPath();
                            ctx.moveTo(0, 8);
                            ctx.lineTo(280 , 8);
                            ctx.stroke();
                        }
                    }
                }

                handle: Rectangle {
                    // anchors.verticalCenter: parent.verticalCenter
                    width: 6
                    height: 12
                    color: "#2F2F2F"
                    radius: 3
                    x: (300 - 20 - width) * (customSlider.value / customSlider.to)
                }
            }

            Rectangle {

                radius: 8
                color: "#F3F4F6"
                Layout.preferredHeight: 28
                Layout.preferredWidth: 40

                Text {
                    anchors.centerIn: parent

                    text: {

                        var value = customSlider.value;
                        switch(value) {
                        case 8: clPreviewView.speedScale = 40; return  "40x "
                        case 7: clPreviewView.speedScale = 20; return  "20x "
                        case 6: clPreviewView.speedScale = 10; return  "10x "
                        case 5: clPreviewView.speedScale = 5; return   "5x   "
                        case 4: clPreviewView.speedScale = 2; return   "2x   "
                        case 3: clPreviewView.speedScale = 1; return   "1x   "
                        case 2: clPreviewView.speedScale = 0.5; return "0.5x"
                        case 1: clPreviewView.speedScale = 0.2; return "0.2x"
                        case 0: clPreviewView.speedScale = 0.1; return "0.1x"
                        }
                    }

                }
            }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值