View Full Version : [SOLVED] QTreeView drawing selection with icons

21st January 2006, 15:22
Hi all,

I'm trying to display 3 icons in the first 3 columns of a QTreeView. I did this by creating a model and returning a QIcon in the DecorationRole of the data function (the icon depends on the data of the item). This works quite ok but when selecting a row the selection looks quite ugly imo (as you can see in the first pic I attached). Because of the icons are placed very close to each other the selection looks more like a separator than a selection and I'm looking for a solution like in the second pic attached (taken from eMule) where the selection surrounds the icon.

The problem I'm facing now is that I don't really know where to start.

I think either my approach to use the decoration is not that good and drawing the icon in another way (but which one ?) would solve ths problem automatically ...


I have to reimplement the QTreeView class and handle drawing the selection by myself. But also here I'm not really sure where to start.

any ideas about this ?

21st January 2006, 18:36
Leave the treeview alone for now. I think reimplementing the delegate (its paint method) will suffice. If not, reimplement QTreeView::drawRow().

22nd January 2006, 14:27
Ok got it working now. Reimplementing the delegate worked.
drawDecoration() only colors the pixmap itself (not the remaining space)
when a row is selected in qtemdelegate.

23rd January 2006, 13:19
Hi Timewarp. I see that you had a problem similar that mine. I have inserted an example of my problem. Is like your a difference that in my case the icons are in different columns. But how you can see the icon of the book when is selected looks bad. I don't know why because the background of the icon is transparent. Do you or anybody else know how to solve this? Any example of code will be apreciated. Thanks.

23rd January 2006, 19:47
my icons are in different columns too so I would say this is exactly the same problem I had.
I solved it by deriving my own delegate class from QItemDelegate and reimplemented the paint() function. If you have a look at the paint function (qitemdelegate.cpp) you can see that the drawing of the text (drawDisplay) and icons (drawDecoration) is done in the last lines.
I replaced this code by mine.

:eek: But when looking at it closer this works only for columns with just an icon.
When it worked I hadn't looked at it closer. The text will be printed below the icon in this case.

[Edit2] I removed the not complete working code but this should work better now:

// draw the item
drawCheck(painter, opt, checkRect, checkState);

QRect newRect;

newRect.setLeft(pixmapRect.left() );

drawDecoration(painter, opt, pixmapRect, pixmap, newRect);
drawDisplay(painter, opt, textRect, text);
drawFocus(painter, opt, textRect);

//new drawDecoration
void ServerDelegate::drawDecoration(QPainter *painter, const QStyleOptionViewItem &option,
const QRect &rect, const QPixmap &pixmap, const QRect &newRect) const
if(!pixmap.isNull() && !rect.isEmpty())
if(option.state & QStyle::State_Selected)
bool enabled = option.state & QStyle::State_Enabled;
QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;

QPixmap *pm = selected(pixmap, option.palette, enabled);
painter->fillRect(newRect, option.palette.brush(cg, QPalette::Highlight));
painter->drawPixmap(rect.topLeft(), *pm);
painter->drawPixmap(rect.topLeft(), pixmap);

24th January 2006, 09:54
Many thanks Timewrap. I will try it.

27th January 2006, 18:49
Hi, only comment that this problem is automaticly solved in the new version of Qt (4.1.0)

7th February 2013, 08:52
Hi, Can u provide me code to display 3 icon in a row?