Qt QML滑块在开始和结束处用文本标记

Qt QML滑块在开始和结束处用文本标记,qt,qml,qqmlcomponent,Qt,Qml,Qqmlcomponent,如何实现这样的目标。 薄文本和厚文本是否必须作为标签在滑块外部,或者它们是否可以作为记号的一部分?看起来它们不可能是记号的一部分,但您可以通过单独的文本标签轻松实现这一点: Slider { id: slide width: 200 } Text { text: "THIN" anchors.top: slide.bottom anchors.left: slide.left } Text { text: "THICK"

如何实现这样的目标。
薄文本和厚文本是否必须作为标签在滑块外部,或者它们是否可以作为记号的一部分?

看起来它们不可能是记号的一部分,但您可以通过单独的文本标签轻松实现这一点:

 Slider {
    id: slide
    width: 200
  }

  Text {
    text: "THIN"
    anchors.top: slide.bottom
    anchors.left: slide.left
  }
  Text {
    text: "THICK"
    anchors.top: slide.bottom
    anchors.right: slide.right
  }

它们似乎不可能是记号标记的一部分,但您可以通过单独的文本标签轻松实现这一点:

 Slider {
    id: slide
    width: 200
  }

  Text {
    text: "THIN"
    anchors.top: slide.bottom
    anchors.left: slide.left
  }
  Text {
    text: "THICK"
    anchors.top: slide.bottom
    anchors.right: slide.right
  }

这可以通过样式轻松完成。我建议您查看
$QTHOME/QML/QtQuick/controls[/styles/Base]
中的QML控件/样式源代码,以了解QML控件的默认样式

import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.4


Window {
    id: rootWindow
    visible: true
    height: 800
    width: 800

    Rectangle {
        width: 350
        height: 100
        color: "#555"
        anchors.centerIn: parent
        Slider {
            anchors.centerIn: parent
            minimumValue: 1
            maximumValue: 5
            stepSize: 1
            tickmarksEnabled: true
            width: 300
            style: SliderStyle {
                handle: Rectangle {
                    width: 18
                    height: 30
                    border.width: 2
                    border.color: "#555"
                    color: "#CCC"
                    radius: 5
                }
                groove: Rectangle {
                    height: 15
                    width: parent.width
                    anchors.verticalCenter: parent.verticalCenter
                    color: "#CCC"
                }
                tickmarks: Repeater {
                    id: repeater
                    model: control.stepSize > 0 ? 1 + (control.maximumValue - control.minimumValue) / control.stepSize : 0

                    Item {
                        Text {
                            y: repeater.height + 5
                            width : repeater.width / repeater.count
                            x: width * index
                            height: 30
                            color: "#CCC"
                            font.pixelSize: 20
                            text: getText()
                            horizontalAlignment: getAlign()

                            function getText() {
                                if(index === 0) return "THIN"
                                else if(index === repeater.count - 1) return "THICK"
                                else return "";
                            }
                            function getAlign() {
                                if(index === "0") return Text.AlignLeft
                                else if(index === repeater.count - 1) return Text.AlignRight
                                else return Text.AlignHCenter;
                            }
                        }
                        Rectangle {
                            color: "#CCC"
                            width: 2 ; height: 5
                            y: repeater.height
                            x: styleData.handleWidth / 2 + index * ((repeater.width - styleData.handleWidth) / (repeater.count-1))
                        }
                    }
                }
            }
        }
    }
}

exampe中充满了过多的毫无价值的代码,但这有助于理解。

这可以通过样式轻松完成。我建议您查看
$QTHOME/QML/QtQuick/controls[/styles/Base]
中的QML控件/样式源代码,以了解QML控件的默认样式

import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.4


Window {
    id: rootWindow
    visible: true
    height: 800
    width: 800

    Rectangle {
        width: 350
        height: 100
        color: "#555"
        anchors.centerIn: parent
        Slider {
            anchors.centerIn: parent
            minimumValue: 1
            maximumValue: 5
            stepSize: 1
            tickmarksEnabled: true
            width: 300
            style: SliderStyle {
                handle: Rectangle {
                    width: 18
                    height: 30
                    border.width: 2
                    border.color: "#555"
                    color: "#CCC"
                    radius: 5
                }
                groove: Rectangle {
                    height: 15
                    width: parent.width
                    anchors.verticalCenter: parent.verticalCenter
                    color: "#CCC"
                }
                tickmarks: Repeater {
                    id: repeater
                    model: control.stepSize > 0 ? 1 + (control.maximumValue - control.minimumValue) / control.stepSize : 0

                    Item {
                        Text {
                            y: repeater.height + 5
                            width : repeater.width / repeater.count
                            x: width * index
                            height: 30
                            color: "#CCC"
                            font.pixelSize: 20
                            text: getText()
                            horizontalAlignment: getAlign()

                            function getText() {
                                if(index === 0) return "THIN"
                                else if(index === repeater.count - 1) return "THICK"
                                else return "";
                            }
                            function getAlign() {
                                if(index === "0") return Text.AlignLeft
                                else if(index === repeater.count - 1) return Text.AlignRight
                                else return Text.AlignHCenter;
                            }
                        }
                        Rectangle {
                            color: "#CCC"
                            width: 2 ; height: 5
                            y: repeater.height
                            x: styleData.handleWidth / 2 + index * ((repeater.width - styleData.handleWidth) / (repeater.count-1))
                        }
                    }
                }
            }
        }
    }
}

exampe中充满了大量无用的代码,但这有助于理解。

我还没有测试过这段代码,这段代码看起来肯定不是无用的,也正是我想要的。tickmarksEnabled:true在这里起主要作用,这在qt文档中没有提到。谢谢。我还没有测试过这段代码,这段代码看起来并不是一文不值的,也正是我想要的。tickmarksEnabled:true在这里起主要作用,这在qt文档中没有提到。谢谢。