-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtmldelegate.cpp
36 lines (28 loc) · 1.11 KB
/
htmldelegate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "htmldelegate.h"
#include <QStyleOptionViewItemV4>
#include <QTextDocument>
void HtmlDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QStyleOptionViewItem options = option;
initStyleOption(&options, index);
painter->save();
QTextDocument doc;
doc.setHtml(options.text);
/* Call this to get the focus rect and selection background. */
options.text = "";
options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter, options.widget);
/* Draw using our rich text document. */
painter->translate(options.rect.left(), options.rect.top());
QRect clip(0, 0, options.rect.width(), options.rect.height());
doc.drawContents(painter, clip);
painter->restore();
}
QSize HtmlDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QStyleOptionViewItem options = option;
initStyleOption(&options, index);
QTextDocument doc;
doc.setHtml(options.text);
doc.setTextWidth(options.rect.width());
return QSize(doc.idealWidth(), doc.size().height());
}