PDA

View Full Version : Fixed: Delegate stops Model data background role color being painted



Jeffb
3rd February 2012, 03:05
Hi Guys

I'm applying a delegate to a treeView but when I do it cancels out the background role of the data in the model represented by QModelIndex.
If I get the QBrush value of the background role and then try the code below, it still doesn't fill in the background of the cell with the color.

Can anyone tell me how to fix this?

Thanks
Jeff



void JB_EntireTreeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{

QString currDescription = index.model()->data(index, Qt::DisplayRole).toString();
QBrush backgroundBrush = index.model()->data(index, Qt::BackgroundRole).value<QBrush>();

QStyleOptionViewItem myOption = option;
QString dataDescription = QString("%1 %2").arg(globalVal, currDescription);
myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;

// none of these set the background color back to that of the index background role
// myOption.palette.setBrush(QPalette::Window, backgroundBrush);
// or
// myOption.palette.setBrush(QPalette::Base, backgroundBrush);
// or
painter->setBackground(backgroundBrush);

drawDisplay(painter, myOption, myOption.rect, dataDescription);
}

Added after 1 1:

Fixed.

I didn't realize I needed QItemDelegate drawbackground as well.

Cheers
Jeff