0

I am using QCombobox, i want to every item in QCombobox displays three icons. But currently, every item in QCombobox only displays one icon!

Every icon should be changed dynamically.

demonplus
  • 5,613
  • 12
  • 49
  • 68
Anglejoy
  • 63
  • 1
  • 8

2 Answers2

2

Reimplement paintEvent, or use big icon image with all 3 icons on it.

Jeka
  • 1,364
  • 17
  • 33
2

You should create new custom QAbstractItemDelegate and set it to QComboBox using void QComboBox::setItemDelegate ( QAbstractItemDelegate * delegate ) api.

In delegate, you need to implement

virtual void    paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const = 0

as you required.

You will also need to use following API to provide different icon to combo box in userData, that you can use in deletegate's paint method to retrieve icon and draw it.

void QComboBox::addItem ( const QString & text, const QVariant & userData = QVariant() )

Summary:

When I implement as above, there icons only show as drop down list clicked. In normal situation, the text only show. So, for three icons and text show in normal situation we must reimplement paintEvent of QCombobox in case subclass QCombobox or using eventFilter to catch paintEvent of QCombobox without subclass QComboBox! Thank for your all response!

Anglejoy
  • 63
  • 1
  • 8
Kunal
  • 3,475
  • 21
  • 14
  • Currently my three Icons only show when I click on dropdown list of QCombobox, Item only shows text. I want text and three Icons show on current item instead of clicking. – Anglejoy Apr 26 '12 at 03:49
  • you mean you see three on drop down list is on but not in normal situation ? – Kunal Apr 26 '12 at 05:11
  • for that you need to override "paintEvent()" of QComboBox as suggesed by @jeka – Kunal Apr 26 '12 at 08:04