如何在QtControls 2.0中设置SpinBox的对齐方式?

如何在QtControls 2.0中设置SpinBox的对齐方式?,qt,qml,qtquick2,qtquickcontrols2,Qt,Qml,Qtquick2,Qtquickcontrols2,默认情况下,“我的微调框”中文本的文本对齐方式居中。状态为存在水平对齐属性,但当我尝试指定水平对齐时,出现以下错误: 无效的属性名称“horizontalAlignment”。(M16) 我的完整SpinBox代码如下: SpinBox { editable: true horizontalAlignment: Qt.AlignLeft from: 1 to: 10000 value: model.numberOfElements } 如何在Qt Con

默认情况下,“我的微调框”中文本的文本对齐方式居中。状态为存在
水平对齐
属性,但当我尝试指定水平对齐时,出现以下错误:

无效的属性名称“horizontalAlignment”。(M16)

我的完整SpinBox代码如下:

SpinBox {
    editable: true
    horizontalAlignment: Qt.AlignLeft
    from: 1
    to: 10000
    value: model.numberOfElements
}

如何在Qt Controls 2.0中对齐SpinBox中的文本?

首先,您指出的文档链接来自Qt Quick Controls 1的SpinBox,Qt Quick Controls 2的链接是:

考虑到上述Qt Quick Controls 2,它有一个说明如何自定义控件的文档:

在这种情况下,解决方案是:

import QtQuick.Controls 2.5

SpinBox {
    id: control
    value: 50
    editable: true
    contentItem: TextInput {
        z: 2
        text: control.textFromValue(control.value, control.locale)

        font: control.font
        color: "#21be2b"
        selectionColor: "#21be2b"
        selectedTextColor: "#ffffff"
        horizontalAlignment: Qt.AlignLeft
        verticalAlignment: Qt.AlignVCenter

        readOnly: !control.editable
        validator: control.validator
        inputMethodHints: Qt.ImhFormattedNumbersOnly
    }
}