具有枢轴值的QML RangeSlider

问题描述 投票:0回答:1

[我正在使用RangeSlider,并且尝试创建一个枢轴值,其中第一个值不能超过该值,第二个值不能低于该值。

RangeSliderFixed.qml

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1

Item {
    id: root

    property int minValue: 0
    property int maxValue: 100
    property alias firstValue : rangeSlider.first.value
    property alias secondValue : rangeSlider.second.value
    property int step: 1
    property int pivot: maxValue/2

    Row {
        anchors.fill: parent

        Text {
            width: parent.width * 0.15
            height: parent.height
            text: rangeSlider.first.value.toFixed(0)
            horizontalAlignment: Text.AlignRight
            verticalAlignment: Text.AlignVCenter
        }

        RangeSlider {
            id: rangeSlider
            width: parent.width * 0.7
            height: parent.height

            from: minValue
            to: maxValue
            stepSize: step
            snapMode: RangeSlider.SnapAlways

            // binding loop here
            first.value: first.value > pivot ? pivot : first.value
            second.value: second.value < pivot ? pivot : second.value

        }

        Text {
            width: parent.width * 0.15
            height: parent.height
            text : rangeSlider.second.value.toFixed(0)
            horizontalAlignment: Text.AlignLeft
            verticalAlignment: Text.AlignVCenter
        }
    }
}

main.qml

import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    visible: true
    width: 640
    height: 480

    RangeSliderFixed {
        anchors.fill: parent
    }
}

当然,当值超过关键点时会有一个绑定循环,但是我对QML还是陌生的,我不知道如何避免这种情况。

qt qml
1个回答
0
投票

嗯,我想这很简单:

        first.value: minValue
        second.value: maxValue

        first.onValueChanged: {
            var value = first.value > pivot ? pivot : first.value
            first.value = value
        }

        second.onValueChanged: {
            var value = second.value < pivot ? pivot : second.value
            second.value = value
        }

最初,我担心由于属性绑定的中断而使用了赋值,但是实际上,只有在加载组件以匹配初始位置时才需要绑定,所以一切都应该没问题。

© www.soinside.com 2019 - 2024. All rights reserved.