Created
June 11, 2018 23:01
-
-
Save jniemann66/dbc298b35a840bf3f1a2206ea6284c7b to your computer and use it in GitHub Desktop.
Qt Rich Text Delegate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "richtextdelegate.h" | |
#include <QPainter> | |
#include <QTextDocument> | |
RichTextDelegate::RichTextDelegate(QObject *parent) : QStyledItemDelegate(parent) | |
{ | |
} | |
void RichTextDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const | |
{ | |
if (option.state & QStyle::State_Selected) | |
painter->fillRect( option.rect, option.palette.highlight()); | |
painter->save(); | |
QTextDocument document; | |
document.setTextWidth(option.rect.width()); | |
QVariant value = index.data(Qt::DisplayRole); | |
if(value.isValid() && !value.isNull()) | |
{ | |
document.setHtml(value.toString()); | |
painter->translate(option.rect.topLeft()); | |
document.drawContents(painter); | |
} | |
painter->restore(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef RICHTEXTDELEGATE_H | |
#define RICHTEXTDELEGATE_H | |
// richtextdelegate.h : allows rich text to be used inside a table cell | |
#include <QStyledItemDelegate> | |
class RichTextDelegate: public QStyledItemDelegate | |
{ | |
public: | |
RichTextDelegate(QObject *parent = 0); | |
void paint( QPainter *painter, | |
const QStyleOptionViewItem &option, | |
const QModelIndex &index ) const; | |
}; | |
#endif // RICHTEXTDELEGATE_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment