2

I want to set the icon on the left side of the QCombobox widget. I know I can insert a item first and then set the icon of the inserted item and then select this newly inserted item. However, I would like to do that without inserting a new item into the drop down list for special reasons. Windows ComboBox control allows us to change the icon of the edit box by using an index of -1. I don't know how to achieve that with QCombobox.

Thanks for any comments!

Stephen Cheng
  • 964
  • 2
  • 11
  • 24

1 Answers1

1

Never tried it myself, but here is an idea.

QComboBox is based on Qt's model/view framework, so the items are contained into a QStandardItemModel which can be accessed with QComboBox::model().

The steps would be:

  • Instantiate a QStandardItem
  • Use setIcon() and setText() on the QStandardItem (or use the proper ctor)
  • When you want to add the item to the Combo list, append it thru the model.

Example:

QStandardItem* item = new QStandardItem(theIcon, theText);
[...]
QStandardItemModel* comboModel = qobject_cast<QStandardItemModel*>(theCombo->model());
comboModel->appendRow(item);
Chris Browet
  • 4,156
  • 20
  • 17