自定义TextEdit,当它变宽时如何隐藏TextInput

Custom TextEdit, how to hide TextInput when it goes wide

我正在尝试使用 TextInput 元素创建自定义文本字段(我需要它来使用验证器和自定义样式)。但我无法隐藏部分 TextInput 内容展开(见图)。我对其他元素也有类似的问题,虽然它有包含其他项目的根项目(容器),但如果它们被放置在容器坐标之外,则可以看到孩子。如果子组件不在根容器中,我如何隐藏它们?

有代码,但它实际上只是模板,我尝试使用 z 属性,但没有成功。

BreezeQuickLineInput.qml

import QtQuick 2.4

Item {
    id: root
    property int fontSize: 18
    property BreezeQuickPalette palette: BreezeQuickPalette
    property string text: "Type here..."
    implicitHeight: input.font.pixelSize*2
    implicitWidth: 196
    Rectangle{
        id: body
        color: "transparent"
        anchors.fill: parent
        border {
            color: palette.plasmaBlue
            width: 1
        }
        TextInput{
            id: input
            anchors {
                fill: parent
            }
            font.pointSize: fontSize
            color: palette.normalText
            selectByMouse: true
        }
    }
}

感谢任何帮助。我查看了 TextInput 文档,但如果您知道我应该学习什么主题,请告诉我。

嗯,真奇怪什么时候发现layer属性组。我刚刚打开 layer.enabled 并且我的目标已经完成。 Qt 文档中缺少一些信息。不幸的是之前不知道layer组的目的。

BreezeQuickLineInput.qml

import QtQuick 2.4

Item {
    id: root
    property int fontSize: 18
    property BreezeQuickPalette palette: BreezeQuickPalette
    property string text: "Type here..."
    implicitHeight: input.font.pixelSize*2
    implicitWidth: 196
    Rectangle{
        id: body
        color: "transparent"
        anchors.fill: parent
        border {
            color: palette.plasmaBlue
            width: 1
        }
        TextInput{
            id: input
            anchors {
                fill: parent
            }
            font.pointSize: fontSize
            color: palette.normalText
            selectByMouse: true
            layer.enabled: true
        }
    }
}

更新:

我的不好,Qt做生意不错。我的回答在 Item 描述中。来自 Qt 文档:

Item Layers

An Item will normally be rendered directly into the window it belongs to. >However, by setting layer.enabled, it is possible to delegate the item and >its entire subtree into an offscreen surface. Only the offscreen surface, a >texture, will be then drawn into the window.

更新:

使用 Itemclip 属性的 BaCaRoZzo 评论更便宜。

clip : bool

This property holds whether clipping is enabled. The default clip value is false.

If clipping is enabled, an item will clip its own painting, as well as the painting of its children, to its bounding rectangle.

所以,我就把它放在那里了,相信它可以帮助其他有同样问题的人。

只需添加 clip:true 在您的文本输入中。这将解决您的问题。