woodtluk
22nd September 2010, 11:17
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:
void MembersListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
if(!index.isValid())
return;
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
QStyleOptionViewItemV4 opt = option;
QStyledItemDelegate::initStyleOption(&opt, index);
QRect rect = opt.rect;
// 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!
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:
void MembersListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
if(!index.isValid())
return;
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
QStyleOptionViewItemV4 opt = option;
QStyledItemDelegate::initStyleOption(&opt, index);
QRect rect = opt.rect;
// 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!