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

时间:2019-01-23 16:43:17

标签: qt qml qtquick2 qtquickcontrols2

默认情况下,我的SpinBox中文本的文本对齐方式居中。 SpinBox documentation指出有一个horizontalAlignment属性,但是当我尝试指定水平对齐方式时,出现以下错误:

Invalid property name "horizontalAlignment". (M16)

我的完整SpinBox代码在这里:

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

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

1 个答案:

答案 0 :(得分:3)

首先要指出的文档链接来自Qt Quick Controls 1的SpinBox,Qt Quick Controls 2的链接为:https://doc.qt.io/qt-5/qml-qtquick-controls2-spinbox.html

考虑到上述Qt Quick Controls 2,有一个文档指示如何自定义控件:Customizing Qt Quick Controls 2

对于SpinBox,解决方案是:

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
    }
}