Paint selection in QStyledItemDelegate's subclass
Hi there
For a list in my application I use a delegate that is derived from QStyledItemDelegate. In the paint method I'd like to handle the selection of the items in the list.
Now I'm doing it like this:
Code:
if(!index.isValid())
return;
painter->save();
painter
->setRenderHint
(QPainter::Antialiasing);
QStyleOptionViewItemV4 opt = option;
QStyledItemDelegate::initStyleOption(&opt, index);
// handle selection
if(option.
state & QStyle::State_Selected){ painter->save();
QBrush selectionBrush
(Qt
::green);
painter->setBrush(selectionBrush);
painter->drawRoundedRect(rect.adjusted(1,1,-1,-1), 5, 5);
painter->restore();
}
// ...
painter->restore();
}
But the selection doesn't look exactly the same as the default one which is used when no delegate is set. How can I use the standard selection in my delegate.
Btw: I'm using Qt on Symbian (Nokia N8) where the background of the list has black color and the selection is a green rounded rect with some kind of gradient.
Thanks!
Re: Paint selection in QStyledItemDelegate's subclass
I figured it out:
In the paint function of my delegate I first call the paint function of the QStyledItemDelegate.
And then I just don't paint any selection myself anymore.
Code:
if(!index.isValid())
return;
QStyledItemDelegate::paint(painter, option, index);
painter->save();
painter
->setRenderHint
(QPainter::Antialiasing);
QStyleOptionViewItemV4 opt = option;
QStyledItemDelegate::initStyleOption(&opt, index);
// ... All my custom painting stuff here
painter->restore();
}
Cheers Luke
Re: Paint selection in QStyledItemDelegate's subclass
I have solved my problem by setting mouseTracking option in my QTableView.
P.S. Sorry for my english.