C++ (Qt)#ifndef HTMLDELEGATE_H#define HTMLDELEGATE_H #include <QStyledItemDelegate> class HtmlDelegate : public QStyledItemDelegate{ Q_OBJECT public: HtmlDelegate(QObject *parent = 0); void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;}; #endif
C++ (Qt)#include <QtWidgets>#include "htmldelegate.h" HtmlDelegate::HtmlDelegate(QObject *parent) : QStyledItemDelegate(parent){} void HtmlDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItem opt = option; initStyleOption(&opt, index); painter->save(); QLabel doc(opt.text); doc.setText(opt.text); opt.text = ""; opt.widget->style()->drawControl(QStyle::CE_ItemViewItem, &option, painter); painter->translate(opt.rect.left(), opt.rect.top()); QRect clip(0, 0, opt.rect.width(), opt.rect.height()); doc.render(painter, QPoint(0,0), QRegion(QRect(clip)), QWidget::DrawChildren); // !!!!! painter->restore();}
C++ (Qt)#include "htmldelegate.h"#include <QApplication>#include <QtWidgets> int main(int argc, char *argv[]){ QApplication app(argc, argv); QStringListModel model; QTableView tableView; QStringList lst; lst << "<b>First</b> html text" << "Se<font color=red>con</font>d html <i>text</i>"; model.setStringList(lst); tableView.setModel(&model); HtmlDelegate delegate; tableView.setItemDelegate(&delegate); tableView.setEditTriggers(QAbstractItemView::NoEditTriggers); tableView.setWindowTitle("Html delegate"); tableView.show(); tableView.resizeColumnsToContents(); return app.exec();}
C++ (Qt)//............painter->translate(opt.rect.left(), opt.rect.top());QRect clip(0, 0, opt.rect.width(), opt.rect.height()); lbl.render(painter, QPoint(5,5), QRegion(QRect(clip)), QWidget::DrawChildren); // QPoint(5,5)//.........
C++ (Qt)lbl.render(painter, QPoint(5,opt.rect.height()/2 - 7), QRegion(QRect(clip)), QWidget::DrawChildren);