再某些编程语言中,可编辑输入框,可以设置placeholderText属性,即文本输入框中在用户未输入文本时显示的提示文本的属性。
自定义XTextInput.qml文件:
import QtQuick 2.15
import QtQuick.Controls 2.15
//实现自定义TextInput,支持placeholderText属性
TextInput {
property string placeholderText: "Enter text here"
property string inputText: ""
width: 200
height: 32
anchors.leftMargin: 5
anchors.rightMargin: 2
font.pixelSize: 14
verticalAlignment: Text.AlignVCenter
autoScroll: true
text: inputText === "" ? placeholderText : inputText
clip: true
onTextChanged: {
if (text !== "" && text !== placeholderText) {
inputText = text
}
}