37 lines
1.3 KiB
C++
37 lines
1.3 KiB
C++
#include "HtmlDelegate.h"
|
|
|
|
HtmlDelegate::HtmlDelegate(QObject *parent) : QAbstractItemDelegate(parent){}
|
|
|
|
//HtmlDelegate::HtmlDelegate(QObject *parent = nullptr) : QAbstractItemDelegate(parent) {}
|
|
|
|
|
|
void HtmlDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
|
|
QString html = index.model()->data(index, Qt::DisplayRole).toString();
|
|
QTextDocument doc;
|
|
doc.setHtml(html);
|
|
doc.setTextWidth(option.rect.width());
|
|
|
|
painter->save();
|
|
painter->setClipRect(option.rect);
|
|
|
|
if (option.state & QStyle::State_Selected) {
|
|
// 绘制选中项的背景颜色
|
|
painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
|
|
}
|
|
|
|
painter->translate(option.rect.topLeft());
|
|
doc.drawContents(painter, QRectF(QPointF(0, 0), QSizeF(option.rect.size())));
|
|
painter->restore();
|
|
}
|
|
|
|
QSize HtmlDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const {
|
|
QString html = index.model()->data(index, Qt::DisplayRole).toString();
|
|
QTextDocument doc;
|
|
doc.setHtml(html);
|
|
doc.setTextWidth(option.rect.width());
|
|
return QSize(doc.idealWidth(), doc.size().height());
|
|
}
|
|
|
|
|
|
|