[Interest] QML: PlaceholderText on ComboBox (Controls 2)

Mitch Curtis mitch.curtis at qt.io
Mon Sep 2 09:43:54 CEST 2019

> -----Original Message-----
> From: Interest <interest-bounces at qt-project.org> On Behalf Of Carel
> Combrink
> Sent: Sunday, 1 September 2019 9:22 PM
> To: interest <interest at qt-project.org>
> Subject: [Interest] QML: PlaceholderText on ComboBox (Controls 2)
> Hi,
> I have a ComboBox (QtQuick.Controls 2.5, Qt 5.12.4 ) and want to set a
> "placeholderText" on the ComboBox to display when no text is entered.
> The ComboBox does not have "placeholderText" property and overwriting
> the complete contentItem of the ComboBox seems excessive to just do this
> to replicate the exsiting functionality (including read only or editable
> behaviour)
> If I look at the "Fusion/ComboBox.qml" implementation (I am using the
> Fusion Style), I can see that the contentItem is implemented using a
> "TextField" which means it does support this property.
> I have copied the contentItem definition from the implentation and when I
> set the placeholderText it behaves correctly.
> Is it possible to get access to the TextField used by the original
> implemenation and then set the "placeholderText" on it, without the need
> to re-implment the complete item?
> I have tried the following in the onCompleted handler of the ComboBox
> without success:
> {code}
> ComboBox {
>   id: comboCategory_
>   ...
>   Component.onCompleted: {
>     console.log("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
>     console.log( "ContentItem is    : " + comboCategory_.contentItem )
>     console.log( "placeholderText is: " +
> comboCategory_.contentItem.placeholderText )
>     comboCategory_.contentItem.placeholderText = "<category>"
>     console.log( "placeholderText is: " +
> comboCategory_.contentItem.placeholderText )
>     console.log("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
>   }
> }
> {code}
> The ouptut is:
> {code}
> qml: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
> qml: ContentItem is    : QQuickTextField(0x17fe84249a0)
> qml: placeholderText is:
> qml: placeholderText is: <category>
> qml: bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
> {code}
> But the behaviour does not reflect the change.

That should work (and it appears to from the output), so I'm not sure why you don't see the placeholder text, assuming that is the actual issue here.

> Is this something that can be done or should I create a bugreport for a
> feature request on the ComboBox element?

The way you're doing it in onCompleted is the easiest way to do it currently, and it should work. Can you please post a minimal example here so we can see why it's not working first?

As for a feature request, you can create one if you want. I wonder if something like Nils' idea of exposing a textField property would be the best solution to this:


Usually the contentItem is a TextField, but if it's not, it would just be null. The problem is that this same issue likely applies to lots of other controls, and it might end up in a slippery slope of adding these type-specific accessor properties.

> Regards,
> Carel

More information about the Interest mailing list