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

时间:2015-07-04 21:14:49

标签: text input qml children qtquick2

我尝试使用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文档,但如果您知道我应该学习哪个主题,请告诉我。

enter image description here

2 个答案:

答案 0 :(得分:3)

嗯,真的想知道找到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
        }
    }
}

enter image description here

更新:

我的坏,Qt做得好。我的回答是Item描述。来自Qt docs:

  

项目图层

     

项目通常会直接呈现在它所属的窗口中。 &gt;但是,通过设置layer.enabled,可以将项目&gt;其整个子树委托到屏幕外表面。只有屏幕外的表面,即>纹理,才会被绘制到窗口中。

更新:

使用clip Item属性BaCaRoZzo发表评论的费用较低。

  

剪辑:bool

     

此属性保持是否启用剪裁。默认剪辑值为false。

     

如果启用了剪辑,则项目会将其自己的绘画以及其子画面绘制到其边界矩形。

所以,我把它留在那里,相信它可以帮助其他人提出同样的问题。

答案 1 :(得分:2)

添加 夹:真 在你的文字输入中