QItemDelegate + QListView : paint
Hi guys,
I'm trying to polish a small tool I wrote and therefore want to implement an item delegate for my list view.
Scenario:
There's a QStandardItemModel which is assigned to the list view. The view also has a delegate.
I just added to items to the model which are shown without any problem.
Issue:
There's something wrong with the hovering background color. Please see my code below.
Code:
painter->setPen(Qt::NoPen);
painter
->setBrush
(QColor(0,
0,
255));
if(option.
state & QStyle::State_MouseOver) {
painter
->setBrush
(QColor(255,
0,
0));
}
painter->drawRect(option.rect);
painter->setPen(fontColor);
painter->drawText(option.rect.left() + 5, option.rect.top() + option.rect.height() / 2 + 4, qvariant_cast<QString>(index.data()));
The item's text is drawn correctly at the desired position. But the background is drawn over both items.
The sizeHint() return this:
Code:
return QSize(option.
rect.
width(),
20);
I'm sure I'm missing something or am doing something wrong.
Could anybody please shed some light? :-)
Thanks in advance.
Re: QItemDelegate + QListView : paint
Have you tried saving and restoring the old values for pen and brush, i.e. not permanently change painter state?
Something like
Code:
const QPen oldPen
= painter
->pen
(),
const QBrush oldBrush
= painter
->brush
();
// your code
painter->setPen(oldPen);
painter->setBrush(oldBrush);
Cheers,
_
Re: QItemDelegate + QListView : paint
Well, the issue is not related to wrong painter states but rather to the area where the painter draws the rectangle for the item background.
I assumed that option.rect would deliver the exact boundaries of the hovered item, but if I hover over the first item in the view the second is also highlighted.
Don't know if my explanation makes it clear, please let me know if more details/code is required to help.
Thanks.
Re: QItemDelegate + QListView : paint
Hmm, weird, so option.rect is larger than the cell of that list view entry?
But the text positioning on the same values is correct at the same time?
Cheers,
_
Re: QItemDelegate + QListView : paint
Exactly! That's what makes me think I'm doing something wrong with the background. Just can't see what that would be.
Re: QItemDelegate + QListView : paint
Have you tried not changing the brush but using QPainter::fillRect() instead?
Cheers,
_
Re: QItemDelegate + QListView : paint
I also tried that but still to no avail. So I guess there must be something wrong somewhere else in my delegate class. I've done delegates so many times but maybe I just dont see the wood for the trees. :rolleyes:
Anyway, it's just an esthetic thingy so I won't spend any more time on it right now.
Thanks for your ideas!