5

Without subclassing can I have in QComboBox text that will be shown where no selection was made, something like setPlaceholderText in QLineEdit?

Borrimoro
  • 529
  • 3
  • 9
  • 19
  • Possible Duplicate: http://stackoverflow.com/questions/6327964/qcombox-how-to-set-hint-text-on-combo-box – erelender Aug 16 '13 at 13:58
  • In my window ctor this works: `comboBox->setCurrentIndex(-1);` then I see place holder which I pick in Qt Designer – Gelldur Sep 02 '21 at 07:42

1 Answers1

21

QComboBox does not have a placeholder text option but you can achieve this in two ways:

  1. Add an item with your placeholder text as the first item in the combobox and handle the item selection to account for the extra item.
  2. Use myCombo->lineEdit()->setPlaceholderText("Some text"); But this will only work if your combobox is editable.
Winand
  • 2,093
  • 3
  • 28
  • 48
erelender
  • 6,175
  • 32
  • 49
  • using QtDesigner doesn't work, option #2 does. Thx – Gelldur Jan 07 '21 at 13:54
  • One tiny thing I had to do to make option #2 work, is to do `setCurrentIndex(-1)`. Otherwise it was selecting one of the items by default, instead of showing the placeholder – Nico Villanueva Feb 08 '22 at 17:14