PDA

View Full Version : Paint selection in QStyledItemDelegate's subclass



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!

woodtluk
27th September 2010, 12:22
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.



void MembersListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
if(!index.isValid())
return;

QStyledItemDelegate::paint(painter, option, index);

painter->save();
painter->setRenderHint(QPainter::Antialiasing);

QStyleOptionViewItemV4 opt = option;
QStyledItemDelegate::initStyleOption(&opt, index);
QRect rect = opt.rect;

// ... All my custom painting stuff here


painter->restore();
}




Cheers Luke

vaychick
26th April 2012, 15:51
I have solved my problem by setting mouseTracking option in my QTableView.
P.S. Sorry for my english.