martinn
28th February 2010, 14:39
I have created my own delegate to be able to have a custom style for my QListWidgetItem's. Now I want to use alternatingRowColors for the QListWidgetItem's but I'm not sure how to change the paint method in my delegate to get alternatigRowColors working. How can I change the background color if the row is alternating?
This is how my code for paint method looks like (a little bit simplified):
void ListDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const{
QRect r = option.rect;
if(option.state & QStyle::State_Selected){
painter->setBrush(QBrush(Qt::red, Qt::SolidPattern));
painter->drawRect(r);
} else {
painter->setBrush(QBrush(Qt::white, Qt::SolidPattern));
painter->drawRect(r);
}
//GET TITLE
QString title = index.data(Qt::DisplayRole).toString();
//TITLE
r = option.rect.adjusted(45, 0, -10, -30);
painter->setFont( QFont( "Arial", 6, QFont::Normal ) );
painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignBottom|Qt::AlignLeft, title, &r);
}
This is how my code for paint method looks like (a little bit simplified):
void ListDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const{
QRect r = option.rect;
if(option.state & QStyle::State_Selected){
painter->setBrush(QBrush(Qt::red, Qt::SolidPattern));
painter->drawRect(r);
} else {
painter->setBrush(QBrush(Qt::white, Qt::SolidPattern));
painter->drawRect(r);
}
//GET TITLE
QString title = index.data(Qt::DisplayRole).toString();
//TITLE
r = option.rect.adjusted(45, 0, -10, -30);
painter->setFont( QFont( "Arial", 6, QFont::Normal ) );
painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignBottom|Qt::AlignLeft, title, &r);
}