In order to handle the icon's (decoration) position in the combo box's pull down view, you need to override its view options QAbstractItemView::viewOptions()
. Let's create a custom view and replace the native combo box view with ours:
class ComboView : public QListView
{
protected:
QStyleOptionViewItem viewOptions() const
{
// Set icon on the top and center of combo box item.
QStyleOptionViewItem option = QListView::viewOptions();
option.decorationAlignment = Qt::AlignHCenter | Qt::AlignCenter;
option.decorationPosition = QStyleOptionViewItem::Top;
option.displayAlignment = Qt::AlignCenter;
return option;
}
};
and for the combo box:
QComboBox cb;
cb.setView(new ComboView); // Sets the custom view.
cb.addItem(QIcon("icon.png"), "Item1");
cb.addItem(QIcon("icon.png"), "Item2");
cb.show();