PDA

View Full Version : Background of a selected QItemDelegate



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 );

}
}

schall_l
21st May 2008, 17:11
I just saw that QT4.4 comes with a new class called QStyledItemDelegate.
I am wondering if using QStyledItemDelegate together with a style sheet would be simpler then reimplementing paint ?
Does anyone has any project sample to look at ?

jpn
22nd May 2008, 18:05
I just saw that QT4.4 comes with a new class called QStyledItemDelegate.
I am wondering if using QStyledItemDelegate together with a style sheet would be simpler then reimplementing paint ?
Does anyone has any project sample to look at ?
Yes, it is a lot easier. There are examples in docs:

Customizing QListView (http://doc.trolltech.com/4.4/stylesheet-examples.html#customizing-qlistview)
Customizing QTableView (http://doc.trolltech.com/4.4/stylesheet-examples.html#customizing-qtableview)
Customizing QTreeView (http://doc.trolltech.com/4.4/stylesheet-examples.html#customizing-qtreeview)

schall_l
23rd May 2008, 02:46
Is it possible to define style sheet for a custom class that derives directly from QStyledItemDelegate ?

Something like this don't seems to work.



class SideBarDelegate : public QStyledItemDelegate
{
Q_OBJECT

public:
void paint( QPainter *painter , const QStyleOptionViewItem &option, const QModelIndex &index ) const;
QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const;
};



SideBarDelegate::item:selected:active{
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6ea1f1, stop: 1 #567dbc);
}

SideBarDelegate::item:selected:!active {
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6b9be8, stop: 1 #577fbf);
}

jpn
23rd May 2008, 07:23
You must call the base class implementation; QStyledItemDelegate::paint(). What do you need a reimplementation for?