if(!index.isValid())
return;
painter->save();
painter
->setRenderHint
(QPainter::Antialiasing);
QStyleOptionViewItemV4 opt = option;
QStyledItemDelegate::initStyleOption(&opt, index);
// handle selection
if(option.
state & QStyle::State_Selected){ painter->save();
QPen selectionPen
(option.
palette.
color(QPalette::Highlight));
selectionPen.setWidth(2);
painter->setPen(selectionPen);
painter->drawRoundedRect(rect.adjusted(1,1,-1,-1), 5, 5);
painter->restore();
}
QIcon ic
= QIcon(qvariant_cast<QPixmap>
(index.
data(Qt
::DecorationRole)));
rect = rect.adjusted(4, 4, -4, -4);
/// @todo if ic.isNull() ...
ic.paint(painter, rect, Qt::AlignVCenter|Qt::AlignLeft);
QString txt
= index.
data(Qt
::DisplayRole).
toString();
doc.setHtml(txt);
// icon needs at most 60 pixels in y-axis
rect = rect.adjusted(62, 0, 0, 0);
doc.
setPageSize(QSize(rect.
width(), rect.
height()));
painter->translate(rect.x(),rect.y());
doc.documentLayout()->draw(painter, context);
painter->restore();
}
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
if(!index.isValid())
return;
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
QStyleOptionViewItemV4 opt = option;
QStyledItemDelegate::initStyleOption(&opt, index);
QRect rect = opt.rect;
// handle selection
if(option.state & QStyle::State_Selected){
painter->save();
QPen selectionPen(option.palette.color(QPalette::Highlight));
selectionPen.setWidth(2);
painter->setPen(selectionPen);
painter->drawRoundedRect(rect.adjusted(1,1,-1,-1), 5, 5);
painter->restore();
}
QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
rect = rect.adjusted(4, 4, -4, -4);
/// @todo if ic.isNull() ...
ic.paint(painter, rect, Qt::AlignVCenter|Qt::AlignLeft);
QTextDocument doc;
QAbstractTextDocumentLayout::PaintContext context;
QString txt = index.data(Qt::DisplayRole).toString();
doc.setHtml(txt);
// icon needs at most 60 pixels in y-axis
rect = rect.adjusted(62, 0, 0, 0);
doc.setPageSize(QSize(rect.width(), rect.height()));
painter->translate(rect.x(),rect.y());
doc.documentLayout()->draw(painter, context);
painter->restore();
}
To copy to clipboard, switch view to plain text mode
Bookmarks