如何在 qml 中设置自定义滑块的初始值? [英] How to set initial value of a custom slider in qml?

查看:74
本文介绍了如何在 qml 中设置自定义滑块的初始值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Qt 5.4.1.我制作了一个自定义滑块元素,用于其他 qml 组件,如下所示:

I am using Qt 5.4.1. I have made a custom slider element to be used in other qml components like so:

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 我正在使用这个滑块

And in another file test.qml I am using this slider like so

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 开始,我没有想.我不明白我在这里做错了什么!

I want to set the slider to an initial value when instantiated in test.qml, using the property val. But when I set initial value, onValChanged does not get triggered when sliding the slider. But when I comment that line out (val: 500), onValChanged is triggered when the slider is slid, but the slider starts with initial value of 0 which I don't want. I don't understand what I am doing wrong here!

推荐答案

将属性 val 设置为特定值 覆盖 绑定,如您在 <代码>滑块组件.一旦绑定丢失,滑块的任何更新都不会传递给 val,从而导致您遇到的行为.反之,如果您不设置属性,绑定将保持不变,即随着滑块值的变化,val 的值也会相应变化,从而触发信号.

The setting of the property val to a specific value overrides the binding, as defined in your slider component. Once the binding is lost, any update of the slider is not delivered to val resulting in the behaviour you experienced. On the other way around, if you don't set the property the binding is maintained, i.e. as the slider value changes the value of val changes accordingly, triggering the signal.

在这种情况下,这不是要走的路,还因为您正在添加一组属性,这些属性只是公开了 Slider 的内部属性.只需使用属性 alias:

That's not the way to go in this case, also because you are adding a set of properties which simply exposes inner properties of Slider. Just use properties alias:

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

Property aliases are properties which hold a reference to another property. Unlike an ordinary property definition, which allocates a new, unique storage space for the property, a property alias connects the newly declared property (called the aliasing property) as a direct reference to an existing property (the aliased property).

Slider.qml 中重写你的属性如下:

Rewrite your properties inside Slider.qml as follows:

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

这种方式 val is slider.value 并将其设置为 500 直接影响滑​​块而不会破坏任何绑定.

This way val is slider.value and setting it to 500 directly affect the slider without breaking any binding.

在旁注中,您也可以例如写

On a sidenote, you can also for example write

property alias maximumValue: slider.maximumValue

即使用它们的非常相同的名称公开内部属性,以保持 API 命名的一致性.

i.e. expose inner properties with their very same name to maintain the consistency in API naming.

这篇关于如何在 qml 中设置自定义滑块的初始值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆