如果超出范围,如何在 QML TextInput 中隐藏文本

How to hide text in QML TextInput if it is out of bounds

如果文本的宽度大于 Rectangle id: 字段,我会尝试隐藏它。但它不是“开箱即用”的。如果文本的宽度大于矩形(TextInput 位置)的宽度,我该如何隐藏文本?

    Rectangle {
        id: field
        border.color: "green"
        border.width: 2
        anchors.centerIn: parent
        width:  root.width / 2
        height: 30

        TextInput {
            id: textEdit1

            anchors.verticalCenter: parent.verticalCenter
            anchors.right: okButton.left
            width: field.width - okButton.width
            height: field.height
            text: "Entered"
            padding: 3
            verticalAlignment: TextInput.AlignVCenter
            horizontalAlignment: TextInput.AlignLeft
        }

        Button {
            id: okButton

            width: field.width * 0.15
            height: parent.height
            anchors.right: field.right
            anchors.verticalCenter: field.verticalCenter
            text: "Ok"
        }

您可以启用矩形的 clip 属性:

Rectangle {
    id: field
    clip: true
    // ...