qml: невозможно получить доступ к styleData в компоненте слайдера

Мне нужно значение ширины ручки ползунка, но даже если я просто скопирую код примера из документа Qt, отладчик все равно скажет мне:

Не удается прочитать свойство 'handleWidth' из null

Что я сделал не так?

Мой код, как показано ниже

import QtQuick 2.0
import QtQuick.Controls.Styles 1.4
import QtQuick.Controls 1.4

Slider {
    anchors.centerIn: parent

    style: SliderStyle {
        groove: Rectangle {
            implicitWidth: 200
            implicitHeight: 8
            color: "gray"
            radius: 8
        }
        handle: Rectangle {
            anchors.centerIn: parent
            color: control.pressed ? "white" : "lightgray"
            border.color: "gray"
            border.width: 2
            implicitWidth: 34
            implicitHeight: 34
            radius: 12

            Text{
                text:"test"
                anchors.right:parent.right
                anchors.rightMargin:  styleData.handleWidth * 0.3
            }
        }
    }
}

ОБНОВЛЕНИЕ: в конце концов я нашел обходной путь. Использование состояния и изменения свойств позволит нам изменить свойства элемента в свойстве «дескриптор» с уровня ползунка.


person Anna    schedule 22.04.2017    source источник
comment
Если я использую Component.onCompleted для распечатки styleData.handleWidth, я получаю: styleData не определен   -  person Anna    schedule 22.04.2017


Ответы (1)


Это не общедоступное свойство, поскольку оно не задокументировано.

Вот соответствующий источник :

Loader {
    id: grooveLoader
    property QtObject styleData: QtObject {
        readonly property int handlePosition: handleLoader.x + handleLoader.width/2
    }
    x: padding.left
    sourceComponent: groove
    width: (horizontal ? parent.width : parent.height) - padding.left - padding.right
    y:  Math.round(padding.top + (Math.round(horizontal ? parent.height : parent.width - padding.top - padding.bottom) - grooveLoader.item.height)/2)
}
Loader {
    id: tickMarkLoader
    anchors.fill: parent
    sourceComponent: control.tickmarksEnabled ? tickmarks : null
    property QtObject styleData: QtObject { readonly property int handleWidth: control.__panel.handleWidth }
}
Loader {
    id: handleLoader
    sourceComponent: handle
    anchors.verticalCenter: grooveLoader.verticalCenter
    x: Math.round((control.__handlePos - control.minimumValue) / (control.maximumValue - control.minimumValue) * ((horizontal ? root.width : root.height) - item.width))
}

Обратите внимание, что для handleLoader не объявлен объект styleData.

Делегат handle определяет размер дескриптора ползунка, поэтому вы можете присвоить ему значение. В качестве примера см. Реализация базового стиля дескриптора:

property Component handle: Item{
    implicitWidth:  implicitHeight
    implicitHeight: TextSingleton.implicitHeight * 1.2

    FastGlow {
        source: handle
        anchors.fill: parent
        anchors.bottomMargin: -1
        anchors.topMargin: 1
        smooth: true
        color: "#11000000"
        spread: 0.8
        transparentBorder: true
        blur: 0.1

    }
    Rectangle {
        id: handle
        anchors.fill: parent

        radius: width/2
        gradient: Gradient {
            GradientStop { color: control.pressed ? "#e0e0e0" : "#fff" ; position: 1 }
            GradientStop { color: "#eee" ; position: 0 }
        }
        Rectangle {
            anchors.fill: parent
            anchors.margins: 1
            radius: width/2
            border.color: "#99ffffff"
            color: control.activeFocus ? "#224f7fbf" : "transparent"
        }
        border.color: control.activeFocus ? "#47b" : "#777"
    }
}
person Mitch    schedule 22.04.2017
comment
Спасибо за ответ, причина, по которой мне нужна информация, заключается в том, что содержимое кнопки ручки является динамическим, мне нужно будет настроить другие части графического интерфейса, чтобы они соответствовали изменениям. - person Anna; 26.04.2017