qt - Qt Quick Controls 2 上 SpinBox 的后缀选项

标签 qt qtquickcontrols2

使用 Qt Quick Controls,可以在旋转框中指定后缀(数字的单位),只需编辑属性“suffix”即可。

我想知道这是否适用于 Qt Quick Controls 2.0,因为该属性不可用。关于如何做到这一点的任何建议 customizing the spinbox不费吹灰之力?

最佳答案

更新 - 简单的方法

import QtQuick 2.9
import QtQuick.Controls 2.2

SpinBox{
    width: 180
    height: 40
    from: 1
    to: 12
    textFromValue: function(value, locale) {
                            return (value === 1 ? qsTr("%1 hour")
                                                : qsTr("%1 hours")).arg(value);
                   }
}

旧答案

我已经创建了自己的自定义 SpinBox。

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3

SpinBox {
    id: control
    property int buttonWidth: 40
    property int buttonHeight: 40
    property string baseColor: "#007194"
    property string suffix: ""

    value: 50
    editable: true

    contentItem: RowLayout{
        z: 2
        TextInput {
            id: txtInput
            Layout.fillWidth: true
            Layout.fillHeight: true
            text: control.textFromValue(control.value, control.locale)
            font: control.font
            color: Qt.darker(baseColor, control.enabled ? 1 : 0.7)
            selectionColor: baseColor
            selectedTextColor: "#ffffff"
            horizontalAlignment: Qt.AlignHCenter
            verticalAlignment: Qt.AlignVCenter

            readOnly: !control.editable
            validator: control.validator
            inputMethodHints: Qt.ImhFormattedNumbersOnly
        }
        Text{
            Layout.preferredWidth: contentWidth
            Layout.fillHeight: true
            z: -1
            font: txtInput.font
            color: txtInput.color
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            text: suffix
        }
    }

    up.indicator: Rectangle {
        x: control.mirrored ? 0 : parent.width - width
        height: parent.height
        implicitWidth: buttonWidth
        implicitHeight: buttonHeight
        color: up.pressed ? "#e4e4e4" : "#f6f6f6"
        border.color: enabled ? baseColor : "#bdbebf"

        Text {
            text: "+"
            font.pixelSize: control.font.pixelSize * 2
            color: Qt.darker(baseColor, control.enabled ? 1 : 0.7)
            anchors.fill: parent
            fontSizeMode: Text.Fit
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }
    }

    down.indicator: Rectangle {
        x: control.mirrored ? parent.width - width : 0
        height: parent.height
        implicitWidth: buttonWidth
        implicitHeight: buttonHeight
        color: down.pressed ? "#e4e4e4" : "#f6f6f6"
        border.color: enabled ? baseColor : "#bdbebf"

        Text {
            text: "-"
            font.pixelSize: control.font.pixelSize * 2
            color: Qt.darker(baseColor, control.enabled ? 1 : 0.7)
            anchors.fill: parent
            fontSizeMode: Text.Fit
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }
    }

    background: Rectangle {
        implicitWidth: 140
        border.color: "#bdbebf"
    }
}

希望它能满足您的需求。

关于qt - Qt Quick Controls 2 上 SpinBox 的后缀选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41879198/

相关文章:

c++ - 尝试使用 Qt 访问 Pastebin 时连接超时

c++ - FixedSize 的小部件可以随意改变它们的大小

c++ - 在一个类中填充一个QGraphicsScene并返回场景指针

QtQuick 模板弹出窗口与 QtQuick Controls 弹出窗口

android - 动态创建 ListModel 列表

qt - 列数可变的 QML Repeater 和 QML Grid Layout

qt - CMake 找不到 Qt4 但随后找到 Qt4

c++ - Qt Creator : add custom build configuration settings in . 专业版

qt - 如何在滚动时向上/向下滑动工具栏?

qt - QML 中的动画进度条