Clickable links in QListWidget with Rich Text
-
Hello everyone,
I am trying to implement a way to have clickable URLs in a QListWidget.
I am using the HTMLDelegate class described in this reply: https://stackoverflow.com/a/1956781
The text is interpreted correctly and it's painted correctly, but the URLs in<a>
tags are not clickable in any way.Example text for an item:
Sample text <a href="http://sample.com">this is an url</a> and <b>this is bold text</b>
Does anyone have any ideas how I can make the URLs clickable?I'm on Qt 6.4, if it is a relevant information.
-
Hi,
If memory serves well, you would implement the editorEvent method and there detect whether the cursor is on top of your link and use QDesktopServices to open it.
-
Hi,
If memory serves well, you would implement the editorEvent method and there detect whether the cursor is on top of your link and use QDesktopServices to open it.
-
Something along the line of:
bool MyItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { if (event->type() == QEvent::MouseButtonRelease) { // check if link text is under cursor if (linkIsUnderCursor) { QString url = index.data().toString(); QDesktopServices::openUrl(QUrl(url)); return true; } } return QStyledItemDelegate.editorEvent(event, model, option, index);
-
Something along the line of:
bool MyItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { if (event->type() == QEvent::MouseButtonRelease) { // check if link text is under cursor if (linkIsUnderCursor) { QString url = index.data().toString(); QDesktopServices::openUrl(QUrl(url)); return true; } } return QStyledItemDelegate.editorEvent(event, model, option, index);
-
That's an information you did not give. In any case, the idea is the same except that you have to find in which part of the text the cursor is hovering. You might be able to leverage the implementation of QLabel to replicate the behaviour here.
-
Something along the line of:
bool MyItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { if (event->type() == QEvent::MouseButtonRelease) { // check if link text is under cursor if (linkIsUnderCursor) { QString url = index.data().toString(); QDesktopServices::openUrl(QUrl(url)); return true; } } return QStyledItemDelegate.editorEvent(event, model, option, index);
@SGaist How can I check that the link is under cursor?
-
@SGaist How can I check that the link is under cursor?
@GayaneAlaverdyan did you check the QLabel implementation as suggested above ?