if(!index.isValid())
return;
painter->save();
// handle selection
if(option.
state & QStyle::State_Selected){ painter
->fillRect
(option.
rect, option.
palette.
color(QPalette::Highlight));
}
// get the oicture for the decoration role
QIcon ic
= QIcon(qvariant_cast<QPixmap>
(index.
data(Qt
::DecorationRole)));
QString txt
= index.
data(Qt
::DisplayRole).
toString();
doc.setHtml(txt);
// rectangle for the icon
QRect r
= option.
rect.
adjusted(2,
2,
-2,
-2);
ic.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft);
// rectangle for the text
r = r.adjusted(60, 0, 0, 0);
doc.drawContents(painter, r);
painter->restore();
}
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
if(!index.isValid())
return;
painter->save();
// handle selection
if(option.state & QStyle::State_Selected){
painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
}
// get the oicture for the decoration role
QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
QTextDocument doc;
QString txt = index.data(Qt::DisplayRole).toString();
doc.setHtml(txt);
// rectangle for the icon
QRect r = option.rect.adjusted(2, 2, -2, -2);
ic.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft);
// rectangle for the text
r = r.adjusted(60, 0, 0, 0);
doc.drawContents(painter, r);
painter->restore();
}
To copy to clipboard, switch view to plain text mode
Bookmarks