前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Qml的Slider控件示例

Qml的Slider控件示例

作者头像
Qt君
发布2019-07-15 16:13:57
2.3K0
发布2019-07-15 16:13:57
举报
文章被收录于专栏:跟Qt君学编程

通过自定义样式美化Slider

  • 起始值0处标识同心圆;
  • 拖动动画变化颜色;
  • 气泡显示数值.

示例源码

代码语言:javascript
复制
import QtQuick 2.0
import "../" as My

Rectangle {
    width: 320; height: 240

    Row {
        anchors.centerIn: parent
        spacing: 20

        Column {
            spacing: 50
            /* handle is rectangle */

            Repeater {

                model: ["red", "blue", "green"]

                My.Slider {
                    id: slider
                    to: 100

                    handle: Item {
                        width: 14
                        height: width

                        Rectangle {
                            width: 14
                            height: width
                            radius: width/2
                            color: slider.position === 0 ? "#d5d5d5" : modelData
                            anchors.verticalCenter: parent.verticalCenter

                            NumberAnimation on width { running: slider.pressed; from: 14; to: 20; duration: 100 }
                            NumberAnimation on width { running: !slider.pressed; from: 20; to: 14; duration: 100 }

                            Rectangle {
                                anchors.centerIn: parent
                                width: 10; height: width
                                radius: width/2
                                visible: slider.position === 0
                                color: "white"

                                NumberAnimation on width { running: slider.pressed; from: 10; to: 14; duration: 100 }
                                NumberAnimation on width { running: !slider.pressed; from: 14; to: 10; duration: 100 }
                            }

                            Image {
                                anchors.horizontalCenter: parent.horizontalCenter
                                anchors.bottom: parent.top
                                width: 0; height: width
                                smooth: true
                                source: "./" + modelData + "_64_64.svg"

                                NumberAnimation on width { running: slider.pressed; from: 0; to: 48; duration: 100 }
                                NumberAnimation on width { running: !slider.pressed; from: 48; to: 0; duration: 100 }

                                Text {
                                    visible: slider.pressed
                                    anchors.centerIn: parent
                                    color: "white"
                                    text: parseInt(slider.position * slider.to)
                                }
                            }
                        }
                    }

                    background: Rectangle {
                        width: 200
                        height: 2
                        radius: 2
                        color: "#d5d5d5"

                        /* available rectangle */
                        Rectangle {
                            width:  slider.position*parent.width
                            height: parent.height
                            radius: parent.radius
                            color: modelData
                        }
                    }
                }
            }
        }
    }
}

源码地址

代码语言:javascript
复制
https://github.com/QtComponent/Slider.git
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-03-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Qt君 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 示例源码
  • 源码地址
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档