Skip to content

Instantly share code, notes, and snippets.

@jniemann66
Created June 11, 2018 23:01
Show Gist options
  • Save jniemann66/dbc298b35a840bf3f1a2206ea6284c7b to your computer and use it in GitHub Desktop.
Save jniemann66/dbc298b35a840bf3f1a2206ea6284c7b to your computer and use it in GitHub Desktop.
Qt Rich Text Delegate
#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();
}
#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