How to customize QCombobox with multiple comlumns

杀马特。学长 韩版系。学妹 提交于 2019-12-12 02:56:46

问题


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.


回答1:


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!




回答2:


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



来源:https://stackoverflow.com/questions/10295950/how-to-customize-qcombobox-with-multiple-comlumns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!