Qt 如何在qml中设置自定义滑块的初始值?

Qt 如何在qml中设置自定义滑块的初始值?,qt,slider,qml,Qt,Slider,Qml,我正在使用Qt5.4.1。我制作了一个自定义滑块元素,用于其他qml组件,如: Slider.qml import QtQuick 2.4 import QtQuick.Controls 1.3 import QtQuick.Controls.Styles 1.3 Item { id: root width: 150 height: 30 property int val: slider.value property int maxVal: slide

我正在使用Qt5.4.1。我制作了一个自定义滑块元素,用于其他qml组件,如:

Slider.qml
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Controls.Styles 1.3

Item {
    id: root
    width: 150
    height: 30

    property int val: slider.value
    property int maxVal: slider.maximumValue
    property int minVal: slider.minimumValue
    property int step: slider.stepSize

    Slider {
        id: slider
        anchors.margins: 20
        stepSize: step
        maximumValue: maxVal
        minimumValue: minVal
        style: customStyle
//      onValueChanged: print("From Slider.qml" ,value)
    }

    Component {
        id: customStyle
        SliderStyle {
            handle: Rectangle {
                width: 20
                height: 12
                antialiasing: true
                color: Qt.lighter("#468bb7", 1.2)
            }

            groove: Item {
                implicitHeight: root.height
                implicitWidth: root.width
                Rectangle {
                    height: 8
                    width: parent.width
                    anchors.verticalCenter: parent.verticalCenter
                    color: "#847878"
                    opacity: 0.8
                    Rectangle {
                        antialiasing: true
                        radius: 1
                        color: "#1a0d0d"
                        height: parent.height
                        width: parent.width * control.value / control.maximumValue
                    }
                }
            }
        }
    }
}
在另一个文件
test.qml
中,我像这样使用这个滑块

import QtQuick 2.3

Rectangle {
    id: test

    width: 640; height: 480

    Slider {
        id: slider
        width: 300
        height: 30
        anchors.centerIn: parent
        maxVal: 1000
        minVal: 0
        step: 50
        val: 500 // when commented out, onValChanged is triggered on sliding
        onValChanged: print(val)
    }
}
test.qml
import QtQuick 2.3

Rectangle {
    id: test

    width: 640; height: 480

    Slider {
        id: slider
        width: 300
        height: 30
        anchors.centerIn: parent
        maxVal: 1000
        minVal: 0
        step: 50
        val: 500 // when commented out, onValChanged is triggered on sliding
        onValChanged: print(val)
    }
}

当在
test.qml
中实例化时,我想使用属性
val
将滑块设置为初始值。但是当我设置初始值时,
onValChanged
在滑动滑块时不会被触发。但是,当我注释该行(
val:500
)时,滑动滑块时会触发
onValChanged
,但滑块以初始值0开始,我不希望这样。我不明白我做错了什么

将属性
val
设置为特定值将覆盖
滑块组件中定义的绑定。一旦绑定丢失,滑块的任何更新都不会传递到
val
,从而导致您体验到的行为。另一方面,如果不设置属性,绑定将保持不变,即当滑块值更改时,
val
的值相应更改,从而触发信号

import QtQuick 2.3

Rectangle {
    id: test

    width: 640; height: 480

    Slider {
        id: slider
        width: 300
        height: 30
        anchors.centerIn: parent
        maxVal: 1000
        minVal: 0
        step: 50
        val: 500 // when commented out, onValChanged is triggered on sliding
        onValChanged: print(val)
    }
}
在本例中,这不是解决方法,还因为您添加了一组属性,这些属性只是公开了
滑块的内部属性。仅使用属性:

属性别名是包含对另一个属性的引用的属性。与为属性分配新的唯一存储空间的普通属性定义不同,属性别名将新声明的属性(称为别名属性)连接起来,作为对现有属性(别名属性)的直接引用

Slider.qml
中重写属性,如下所示:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Controls.Styles 1.3

Item {
    id: root
    width: 150
    height: 30

    property int val: slider.value
    property int maxVal: slider.maximumValue
    property int minVal: slider.minimumValue
    property int step: slider.stepSize

    Slider {
        id: slider
        anchors.margins: 20
        stepSize: step
        maximumValue: maxVal
        minimumValue: minVal
        style: customStyle
//      onValueChanged: print("From Slider.qml" ,value)
    }

    Component {
        id: customStyle
        SliderStyle {
            handle: Rectangle {
                width: 20
                height: 12
                antialiasing: true
                color: Qt.lighter("#468bb7", 1.2)
            }

            groove: Item {
                implicitHeight: root.height
                implicitWidth: root.width
                Rectangle {
                    height: 8
                    width: parent.width
                    anchors.verticalCenter: parent.verticalCenter
                    color: "#847878"
                    opacity: 0.8
                    Rectangle {
                        antialiasing: true
                        radius: 1
                        color: "#1a0d0d"
                        height: parent.height
                        width: parent.width * control.value / control.maximumValue
                    }
                }
            }
        }
    }
}
property alias val: slider.value
property alias maxVal: slider.maximumValue
property alias minVal: slider.minimumValue
property alias step: slider.stepSize
这样
val
就是
滑块。value
并将其设置为
500
直接影响滑块,而不会破坏任何绑定

在旁注上,您还可以编写示例

property alias maximumValue: slider.maximumValue
i、 e.公开具有相同名称的内部属性,以保持API命名的一致性