前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >兼容Qt4/Qt5版本Qml控件Slider

兼容Qt4/Qt5版本Qml控件Slider

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

通过滑动滑块选择一个值

  • 导入方法: 文件导入
  • 兼容性: QtQuick1.xQtQuick2.x
  • 继承: Item

属性

  • background: Item
  • from: real
  • handle: Item
  • orientation: enumeration
  • position: real
  • pressed: bool
  • stepSize: real
  • to: real
  • value: real

方法

  • function decrease()
  • function increase()

描述

通过滑动滑块选择一个值

代码语言:javascript
复制
Slider {
    value: 0.5
}

示例

属性文档

  • background:Item 该属性保留着滑动选择控件的背景元素.
  • from:real 该属性保留着范围的起始值.默认值为0.0. 更多相关请查看to和value.
  • handle:Item 该属性保留着滑动滑块的元素.
  • orientation:enumeration 该属性保留着滑动条方向. 可能的值:

描述

Qt.Horizontal

水平方向(默认)

Qt.Vertical

垂直方向

  • [只读属性]position:real 该属性保留着滑块的逻辑位置.逻辑位置为百分比值(0.0-1.0)拖动滑块该值会不断更新.
  • pressed:bool 该属性保留着是否按下滑块.
  • stepSize:real 该属性保留着滑块每次的步长. 更多相关请查看increase()和decrease()
  • to:real 该属性保留着范围的结束值.默认值为1.0. 更多相关请查看to和value.
  • value:real 该属性的值在from到to范围内.默认值为1.0. 该属性与position属性不一样,value为实际值,而position为百分比值;value属性在滑动过程中不会更新值,在释放滑块后才会更新. 更多相关请查看position.

方法文档

  • function decrease() value属性值会按stepSize增加,如果stepSize没有定义则使用默认值增加(0.1). 更多相关请查看stepSize.
  • function increase() value属性值会按stepSize减少,如果stepSize没有定义则使用默认值减少(0.1). 更多相关请查看stepSize.

关于更新

  • 文章首发于微信公众号 你才小学生(nicaixiaoxuesheng)
  • 后续更新于Qtbig哥(qtbig.com)

源码

代码语言:javascript
复制
import QtQuick 2.0

Item {
    id: root
    width: _private.defaultWidth
    height: _private.defaultHeight
    rotation: orientation === Qt.Vertical ? 180 : 0

    /* public */
    // This property holds the starting value for the range. The default value is 0.0.
    property real from: 0.0

    // This property holds the end value for the range. The default value is 1.0.
    property real to: 1.0

    // This property holds the value in the range from - to. The default value is 0.0.
    // Unlike the position property, the value is not updated while the handle is dragged. The value is updated after the value has been chosen and the slider has been released.
    property real value: 0.0

    // This property holds the step size. The default value is 0.0.
    property real stepSize: 0

    // This property holds the orientation.
    // note: [Qt.Horizontal(default) | Qt.Vertical]
    property int  orientation: Qt.Horizontal

    // This property holds the logical position of the handle.
    // The position is defined as a percentage of the control's size, scaled to 0.0 - 1.0.
    // Unlike the value property, the position is continuously updated while the handle is dragged.
    // For visualizing a slider, the right-to-left aware visualPosition should be used instead.
    property real position: 0.0 // note: [read-only]

    // This property holds whether the slider is pressed.
    property bool pressed: false

    // This property holds the handle item.
    property Component handle: _private.defaultHandle

    // This property holds the background item.
    property Component background: _private.defaultBackground

    // Decreases the value by stepSize or 0.1 if stepSize is not defined.
    function decrease() {
        var _stepSize = stepSize === 0 ? 0.1 : stepSize
        position -= _stepSize
    }

    // Increases the value by stepSize or 0.1 if stepSize is not defined.
    function increase() {
        var _stepSize = stepSize === 0 ? 0.1 : stepSize
        position += _stepSize
    }

    /* private */
    /* background */
    Loader {
        id: backgroundId
        anchors.centerIn: parent
        sourceComponent: root.background
    }

    /* handle */
    Loader {
        id: handleId
        sourceComponent: root.handle
    }

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        onPressed: root.pressed = true
        onReleased: {
            root.pressed = false
            root.value = root.position*to
        }
        onPositionChanged: {
            root.position = _private.adjustPosition(mouseArea)
        }
        onClicked: {
            root.position = _private.adjustPosition(mouseArea)
        }
    }

    onValueChanged: {
        if (value > to)
            value = to

        if (value < from)
            value = from

        position = value/to
    }

    onPositionChanged: {
        if (position < 0)
            position = 0

        if (position > 1)
            position = 1

        _private.setHandlePosition()
    }

    Component.onCompleted: _private.setHandlePosition()

    QtObject {
        id: _private
        property real availableWidth: 0
        property real pathRadius: orientation === Qt.Vertical ? root.width * 2 / 15 : root.height * 2 / 15
        property real handleRadius: orientation === Qt.Vertical ? root.width * 3 / 5 : root.height * 3 / 5
        property real pathScale: 1
        property real defaultWidth:  orientation === Qt.Horizontal ? 150 : 30
        property real defaultHeight: orientation === Qt.Vertical ? 150 : 30

        property Component defaultHandle: Rectangle {
            width: _private.handleRadius
            height: width
            radius: width / 2
            color: root.pressed ? "#f0f0f0" : "#f6f6f6"
            border.color: "#bdbebf"
        }

        property Component defaultBackground: Rectangle {
            width:  orientation === Qt.Horizontal ? root.width  : _private.pathRadius
            height: orientation === Qt.Vertical   ? root.height : _private.pathRadius
            radius: 2
            color: "#bdbebf"

            /* available rectangle */
            Rectangle {
                width: orientation === Qt.Horizontal ? position*parent.width : parent.width
                height: orientation === Qt.Vertical ? position*parent.height : parent.height
                radius: parent.radius
                color: "#21be2b"
            }
        }

        function adjustPosition(mouseArea) {
            var _backgroundIdValue = 0
            var _mouseValue = 0
            var _rootValue = 0

            if (orientation === Qt.Vertical) {
                _backgroundIdValue = backgroundId.height
                _mouseValue = mouseArea.mouseY
                _rootValue = root.height
            }
            else {
                _backgroundIdValue = backgroundId.width
                _mouseValue = mouseArea.mouseX
                _rootValue = root.width
            }

            var _position = (_mouseValue / _backgroundIdValue)

            if (stepSize === 0)
                return _position

            if (_position > (position + stepSize))
                return position + stepSize

            if (_position < (position - stepSize))
                return position - stepSize

            return position
        }

        function getX(position) {
            if (orientation === Qt.Horizontal) {
                if (((backgroundId.width*position) - handleId.width/2) <= 0) {
                    return 0
                }
                else if (((backgroundId.width*position) + handleId.width/2) >= backgroundId.width) {
                    return backgroundId.width - handleId.width
                }
                else {
                    return ((backgroundId.width*position) - handleId.width/2)
                }
            }
            else {
                return (root.width - handleId.width)/2
            }
        }

        function getY(position) {
            if (orientation === Qt.Vertical) {
                if (((backgroundId.height*position) - handleId.height/2) <= 0) {
                    return 0
                }
                else if (((backgroundId.height*position) + handleId.height/2) >= backgroundId.height) {
                    return backgroundId.height - handleId.height
                }
                else {
                    return ((backgroundId.height*position) - handleId.height/2)
                }
            }
            else {
                return (root.height - handleId.height)/2
            }
        }

        function setHandlePosition() {
            handleId.x = _private.getX(position)
            handleId.y =  _private.getY(position)
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-02-23,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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