QT Quick - TextField背景和边框

时间:2014-08-15 18:54:58

标签: qt background border qml input-field

我从QT开始,尝试使用以下QML代码设置TextField的样式:

property Component textfieldStyle: TextFieldStyle {
    background: BorderImage {
        source: control.focus ? "images/input-border-focused.png" : "images/input-border.png"
        border.left: 0 ; border.right: 0 ; border.top: 0 ; border.bottom: 4
    }
}

我的问题是:有没有办法设置这样的边框,还有一个图标在字段内,就像下面的图片一样?

enter image description here蓝色部分是窗口的背景。

如果是这样,怎么办呢?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

因为你必须为TextFieldStyle使用Component,所以你不限于使用BorderImage,你可以使用例如像这样的Rectangle来创建你自己的Field

TextField {
    style: TextFieldStyle {
        textColor: "black"
        background: Rectangle {
            radius: 2
            border.color: "red"
            border.width: 3

            Image {
                width: 10
                height: 10
                source: "qrc:///inner.png"
                anchors.verticalCenter: parent.verticalCenter
                anchors.left: parent.left
                anchors.leftMargin: 10
            }
        }
    }
}

由于Rectangle具有border属性,因此您可以直接使用它来设置边框。