Search code examples
qtqcombobox

How to customize QCombobox with multiple comlumns


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.


Solution

  • 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!