Well, you can use delegates to render rich text in qtableview with custom delegates overriding the drawing method, for example:
void CHtmlDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItemV4 opt(option); QLabel *label = new QLabel; label->setText(index.data().toString()); label->setTextFormat(Qt::RichText); label->setGeometry(option.rect); label->setStyleSheet("QLabel { background-color : transparent; }"); painter->translate(option.rect.topLeft()); label->render(painter); painter->translate(-option.rect.topLeft()); }
However, he will not make interactive hyperlink links.
You can use the following hack for this. Reinstall the setModel method in your table / list view and use setIndexWidget.
void MyView::setModel(QAbstractItemModel *m) { if (!m) return; QTableView::setModel(m); const int rows = model()->rowCount(); for (int i = 0; i < rows; ++i) { QModelIndex idx = model()->index(i, 1); QLabel *label = new QLabel; label->setTextFormat(Qt::RichText); label->setText(model()->data(idx, CTableModel::HtmlRole).toString()); label->setOpenExternalLinks(true); setIndexWidget(idx, label); } }
In the above example, I will replace column 1 with qlabels. Note that you need to invalidate the display role in the model to avoid data duplication.
In any case, I will be interested in the best solution based on delegates.
crep4ever
source share