PDA

View Full Version : Use delegate to draw different type of items



nifei
19th January 2009, 02:32
Hi, all,
I want to use a custom delegate to draw itemView's item, and items with different role-values should have different style, and I wish this style could be configured by QSS. I'v got that QSS cannot config single item from others, so i hope QSS config most of the items in a view, and for some special items( with a special role-value), re-implement QStyledItemDelegate : : paint(...) and draw it. However, I don't know how to change the styleoption parameter of paint(...), and I wondered why the items are drawn according to the style sheet even if i did nothing in the re-implementation of paint(...). Here is my code

CustomDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index)const
{
// if i did nothing in this function, the items are draw by the view? since they are still draw as the style sheet
if (index.data(Qt::UserRole).toBool())
{
QStyledItemDelegate::paint(painter, option, index);
}
else
{
QStyleOption another_option(option);
// here, i wonder how to change the option to change the style of this item, for example, i hope this item has the same border and font as others, but different background-color, however, this does not work:
another_option.palette.setColor(QPalette::Backgrou nd, QColor(Qt::yellow));

}
}


Thanks in advance for any suggestions and help

aamer4yu
19th January 2009, 14:16
For using different style for different items, you can have a look at QStylePainter documentation