QML TextEdit中的占位符文本

问题描述 投票:4回答:2

我正在寻找一种方法来显示文本提示,说明预期的输入作为用户的建议。以Google搜索栏为例:

enter image description here

是否有我缺少的属性,或者这是必须通过脚本实现的东西?

qml qt-quick
2个回答
13
投票

Qt Quick输入项目中不存在该属性。您可以投票支持here功能。

在此期间,您可以使用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
        }
    }
}

1
投票

这有点旧,但我发现Android构建的另一个必要条件。由于Android只在虚拟键盘中按ok后发送文本编辑信号,因此占位符仍然存在。所以为了避免它,我建议:

TextEdit {
    id: textEdit
    width: 200
    height: 50

    property string placeholderText: "Enter text here..."

    Text {
        text: textEdit.placeholderText
        color: "#aaa"
        visible: !textEdit.text && !textEdit.activeFocus // <----------- ;-)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.