Drawing pixmaps through delegates for qcombobox
-
So I'm trying to add different images to each item of qcombobox by overriding paint functionality. From a hour or so of googling I found I need to subclass a delegate object, override it's paint function, and set that delegate to the combobox. That or use a qicon which didn't work either. I prefer this route though for later flexibility with painting.
So I use addItem("", MyPixmap) to give the image to the item then when paint is called in the delegate I do:
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QPixmap image = index.data().value<QPixmap>(); painter->drawPixmap(0, 0, option.rect.width(), option.rect.height(), image); }I also tried:
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QPixmap image = index.data().value<QPixmap>(); QApplication::style()->drawItemPixmap(painter, option.rect, 0, image); }Neither works just a blank area is displayed. I should say my image is 1x100 image whose height I would like to be stretched to fill the items container/rect. Any idea what I'm doing wrong?
-
Hi
Try
QPixmap image = index.data().value<QPixmap>();
qDebug() <<"image ok" << image.isNull();to check if it get a valid pixmap ?
Anyway,
the docs for addItem says the second parameter (MyPixmap)
is stored in Qt::UserRole
but you read with data with no role and default is
data(int role = Qt::DisplayRole) constso i think you need at least to do
QPixmap image = index.data(Qt::UserRole).value<QPixmap>();
to get the pixmap again.