-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrichtextdelegate.cpp
More file actions
33 lines (32 loc) · 1.14 KB
/
richtextdelegate.cpp
File metadata and controls
33 lines (32 loc) · 1.14 KB
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
#include "richtextdelegate.h"
#include <QPainter>
#include <QTextDocument>
#include <QAbstractTextDocumentLayout>
#include <QTableWidget>
#include <QDebug>
RichTextDelegate::RichTextDelegate(QObject *parent) :
QStyledItemDelegate(parent)
{
}
void RichTextDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
painter->save();
QTextDocument doc;
QVariant value = index.data(Qt::DisplayRole);
QBrush brush = qvariant_cast<QBrush>(index.data(Qt::BackgroundRole));
QString colorString = brush.color().name();
qDebug()<<colorString;
if (value.isValid() && !value.isNull()) {
QString text;
text.append(QString("<div style=\" background-color: %1\"").arg(colorString));
text.append(value.toString());
text.append("</div>");
doc.setHtml(text);
QAbstractTextDocumentLayout::PaintContext context;
doc.setPageSize(option.rect.size());
//painter->setClipRect( option.rect );
painter->translate(option.rect.x(), option.rect.y());
doc.documentLayout()->draw(painter, context);
painter->restore();
}
}