To make the rich text link clickable in QStyledItemDelegate, you need to handle the mouse events. You can override the editorEvent method to check if the mouse is clicked on a link area, and then emit a signal with the link data.
Here is an example implementation of a ClickableRichtextDelegate, which supports clickable links, no-wrap line, elided text and vertical center text:
#include <QStyledItemDelegate>
#include <QTextDocument>
#include <QPainter>
#include <QStyleOption>
#include <QApplication>
#include <QMouseEvent>
class ClickableRichtextDelegate : public QStyledItemDelegate
{
Q_OBJECT
signals:
void linkActivated(const QString& link);
public:
using QStyledItemDelegate::QStyledItemDelegate;
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override
{
QTextDocument document;
document.setHtml(index.data(Qt::DisplayRole).toString());
document.setTextWidth(option.rect.width());
QStyleOptionViewItem option_new = option;
option_new.displayAlignment = Qt::AlignCenter; // Vertical center text
initStyleOption(&option_new, index);
painter->save();
painter->translate(option_new.rect.topLeft());
document.drawContents(painter, QTextOption(Qt::AlignHCenter | Qt::AlignVCenter));
painter->restore();
}
bool editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index) override
{
if (event->type() == QEvent::MouseButtonRelease)
{
QTextDocument document;
document.setHtml(index.data(Qt::DisplayRole).toString());
document.setTextWidth(option.rect.width());
QPointF ClickedPos = static_cast<QMouseEvent*>(event)->pos() - option.rect.topLeft();
QTextCharFormat format = document.charFormatAt(ClickedPos.toPoint());
if (format.isAnchor())
{
QString link = format.anchorHref();
emit linkActivated(link);
return true;
}
}
return QStyledItemDelegate::editorEvent(event, model, option, index);
}
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override
{
QTextDocument document;
document.setHtml(index.data(Qt::DisplayRole).toString());
document.setTextWidth(option.rect.width());
return document.size().toSize();
}
QString displayText(const QString& text, const QLocale&) const override
{
QFont font; // The font used in the text document
QTextDocument document;
document.setDefaultFont(font);
QString display_text = text;
document.setHtml(display_text);
document.setTextWidth(sizeHint(QStyleOptionViewItem(), QModelIndex()).width());
if (document.textWidth() > document.idealWidth()) // If the text is too long
{
display_text = QApplication::style()->elidedText(display_text, Qt::ElideRight, document.idealWidth());
}
return display_text;
}
};
In the ClickableRichtextDelegate, the linkActivated signal is emitted once a link is clicked.
The editorEvent method is overridden to check whether the mouse event is triggered on a link area within the cell, and emit the signal if it does.
Note that, in order to use the ClickableRichtextDelegate, you need to connect the linkActivated signal to your own slot, which will handle the link activation event. You also need to set the text as <a href="http://your_link">your_text</a> to make it a clickable link within the richtext cell.