在Qt Quick输入项上不存在该属性。您可以在此处投票支持该功能。
同时,您可以使用来自Qt Quick Controls 2的 TextArea
。
如果您更愿意使用纯Qt Quick,则可以类似于控件所做的方式,在字段上方添加 Text
项:
import QtQuick 2.5
import QtQuick.Window 2.2
Window {
width: 300
height: 300
visible: true
TextEdit {
id: textEdit
width: 200
height: 50
property string placeholderText: "Enter text here..."
Text {
text: textEdit.placeholderText
color: "#aaa"
visible: !textEdit.text
}
}
}
虽然这个问题有点老,但我发现在Android构建中还有另一个需要解决的问题。 由于Android只在你按虚拟键盘上的“确认”按钮后才发送文本编辑信号,因此占位符仍然存在。为了避免这种情况,我建议:
TextEdit {
id: textEdit
width: 200
height: 50
property string placeholderText: "Enter text here..."
Text {
text: textEdit.placeholderText
color: "#aaa"
visible: !textEdit.text && !textEdit.activeFocus // <----------- ;-)
}
}
TextField
也可以实现这个功能。 - GrecKo