schall_l
19th May 2008, 23:30
Hi,
Is there a simple way to use a gradient for the box displayed as a background of a selected Item in a class that derives from QItemDelegate ?
I was first thinking to reimplement paint by doing something like:
void
MyDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& i ) const
{
// Special treatment for the selected state
if (option.state & QStyle::State_Selected) {
QItemDelegate::paint( painter, option, i );
QRect selectedRect = option.rect;
selectedRect.setLeft(0);
QLinearGradient linearGradient(0, selectedRect.top() , 0, selectedRect.top() + 100);
linearGradient.setColorAt(0.0, QColor(93,148,214));
linearGradient.setColorAt(0.2, QColor(25,86,173));
painter->setBrush(linearGradient);
painter->fillRect(selectedRect, linearGradient);
// But this is not convenient because it makes me need to reimplement also
// the display of the check box, decoration, text,...
.....
.....
} else {
QItemDelegate::drawBackground( painter, option, i );
}
}
Is there a simple way to use a gradient for the box displayed as a background of a selected Item in a class that derives from QItemDelegate ?
I was first thinking to reimplement paint by doing something like:
void
MyDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& i ) const
{
// Special treatment for the selected state
if (option.state & QStyle::State_Selected) {
QItemDelegate::paint( painter, option, i );
QRect selectedRect = option.rect;
selectedRect.setLeft(0);
QLinearGradient linearGradient(0, selectedRect.top() , 0, selectedRect.top() + 100);
linearGradient.setColorAt(0.0, QColor(93,148,214));
linearGradient.setColorAt(0.2, QColor(25,86,173));
painter->setBrush(linearGradient);
painter->fillRect(selectedRect, linearGradient);
// But this is not convenient because it makes me need to reimplement also
// the display of the check box, decoration, text,...
.....
.....
} else {
QItemDelegate::drawBackground( painter, option, i );
}
}