Is there a way to have a placeholder on a combobox?

Viewed 1801

I want to build a ComboBox component that the first time we get to it, it has a text has a placeholder.

For example:

| panel language |

and when i click it i see the options:

| -- English -- |

| Portuguese |

| -- French -- |

If i select one thats what will now appear visible on the combobox

Im using qt 5.12 and still haven't find a way to do that.

With html it's easy to do what i want:

<style>
    select:invalid { color: gray; }
</style>
<form>
    <select required>
        <option value="" disabled selected hidden>Please Choose...</option>
        <option value="0">Open when powered (most valves do this)</option>
        <option value="1">Closed when powered, auto-opens when power is cut</option>
    </select>
</form>

How can a similar thing be done with qml?

1 Answers

It can be done using displayText and currentIndex:

ComboBox {
    currentIndex: -1
    displayText: currentIndex === -1 ? "Please Choose..." : currentText
    model: [
        "Open when powered (most valves do this)",
        "Closed when powered, auto-opens when power is cut"
    ]
}
Related