Legend Legend - 2 months ago 26
C++ Question

How do i add an icon to QComboBox in Qt?

I want to add an icon with text into the

QComboBox
in Qt, how can I implement it?

Please give me an explanation and a code snippet.

Answer

You can use the following APIs:

void QComboBox::insertItem ( int index, const QString & text, const QVariant & userData = QVariant() )
void QComboBox::setItemIcon ( int index, const QIcon & icon )

http://doc.qt.io/qt-5/qcombobox.html#insertItem
http://doc.qt.io/qt-5/qcombobox.html#setItemIcon

As for the code snippet it's as easy as this:

void AddItem(QComboBox* combo, QString itemName, QIcon* icon)
{
    combo->insertItem(0, itemName);
    combo->setItemIcon(0, *icon);
}