11

I am looking for a way to show a text hint stating the expected input as advice for the user. Take the Google search bar for example:

enter image description here

Is there a property I am missing, or is this something that has to be achieved through scripting?

Mitch
  • 23,716
  • 9
  • 83
  • 122
Herr von Wurst
  • 2,571
  • 5
  • 32
  • 53

3 Answers3

23

The property doesn't exist on the Qt Quick input items. You can vote for the feature here.

In the meantime, you can use TextArea from Qt Quick Controls 2.

If you would rather use pure Qt Quick, you can do something similar to what the Controls do and add a Text item above the field:

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
        }
    }
}
Mitch
  • 23,716
  • 9
  • 83
  • 122
  • 2
    [`TextField`](http://doc.qt.io/qt-5/qml-qtquick-controls-textfield.htm) from Qt Quick Controls 1 also does that. – GrecKo Nov 02 '16 at 09:08
5

This is kinda old but I found another necessity for Android builds. Since Android only send the text editing signal after you press ok in virtual keyboard, the placeholder remains there. So to avoid it, I recommend:

TextEdit {
    id: textEdit
    width: 200
    height: 50

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

    Text {
        text: textEdit.placeholderText
        color: "#aaa"
        visible: !textEdit.text && !textEdit.activeFocus // <----------- ;-)
    }
}
Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
2

If you want one line input then why not use TextField ?

Jarod42
  • 203,559
  • 14
  • 181
  • 302
너를 속였다
  • 899
  • 11
  • 26