Legend Legend - 10 months ago 99
C++ Question

How do i add an icon to QComboBox in Qt?

I want to add an icon with text into the

in Qt, how can I implement it?

Please give me an explanation and a code snippet.

Answer Source

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 )


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);