Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "RichTextDelegate.hpp"
- #include <QPainter>
- #include <QTextDocument>
- RichTextDelegate::RichTextDelegate(QObject *parent)
- : QStyledItemDelegate(parent)
- {
- m_pFormatter = new QTextDocument(this);
- }
- RichTextDelegate::~RichTextDelegate()
- {
- }
- 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();
- int alignemtn=index.data(Qt::TextAlignmentRole).toInt();
- m_pFormatter->setHtml(index.data().toString());
- QFont f(index.data(Qt::FontRole).value<QFont>());
- m_pFormatter->setDefaultFont(f);
- QSizeF actualSize (m_pFormatter->size());
- if ((alignemtn & Qt::AlignVCenter) != 0)
- {
- painter->translate(0,(option.rect.height()-actualSize.height())/2);
- } else if ((alignemtn & Qt::AlignBottom) != 0)
- {
- painter->translate(0,(option.rect.height()-actualSize.height()));
- }
- if ((alignemtn & Qt::AlignRight) !=0)
- {
- painter->translate(option.rect.width()-actualSize.width(),0);
- } else if ((alignemtn & Qt::AlignHCenter) !=0)
- {
- painter->translate((option.rect.width()-actualSize.width())/2,0);
- }
- QPixmap pixmap(option.rect.size());
- pixmap.fill(Qt::transparent);
- QPainter p(&pixmap);
- m_pFormatter->drawContents(&p);
- painter->drawPixmap(option.rect, pixmap);
- painter->restore();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement