PDA

View Full Version : Do not paint selection in QStyledItemDelegate



jgirlich
1st March 2013, 19:16
We have a very complicated custom coloring scheme for a delegate in a QTableWidget. We do need the SelectionModel though, so setSelectionMode(QAbstractItemView::NoSelection) is not an option.

I tried to overwrite the paint function like this:



void LineEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItemV4 noSelectionOption(option);
initStyleOption(&noSelectionOption, index);
noSelectionOption.showDecorationSelected = false;

QStyledItemDelegate::paint(painter, noSelectionOption, index);
}


But the Texts in the cells are still drawn with blue background and white foreground.

How can I fix this?

wysota
1st March 2013, 21:30
Remove QStyle::State_Selected from QStyleOption::state

jgirlich
4th March 2013, 11:16
Added one line of code as to your suggestion and it works like a charm now.



void LineEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItemV4 noSelectionOption(option);
initStyleOption(&noSelectionOption, index);
noSelectionOption.showDecorationSelected = false;
noSelectionOption.state &= ~QStyle::State_Selected;

QStyledItemDelegate::paint(painter, noSelectionOption, index);
}


Thanks a lot!

wysota
4th March 2013, 11:33
FYI, this would have been enough:


void LineEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem noSelectionOption(option);
noSelectionOption.state &= ~QStyle::State_Selected;
QStyledItemDelegate::paint(painter, noSelectionOption, index);
}