I want to use QTextEdit
(in read-only mode) to show a clickable hyperlink, I used to do
QTextEdit *textEdit = new QTextEdit;
QTextCursor cursor(textEdit->document());
textEdit->setTextCursor(cursor);
cursor->insertHtml("<a href=\"www.google.com\" >Google</a>");
textEdit->show();
this code will show Google as hyperlink but unable to click.
And if I used
QTextEdit *textEdit = new QTextEdit;
QTextCursor cursor(textEdit->document());
textEdit->setTextCursor(cursor);
QTextCharFormat linkFormat = cursor.charFormat();
linkFormat.setAnchor(true);
linkFormat.setAnchorHref("http://www.google.com");
linkFormat.setAnchorName("Google");
cursor.insertText("Google", linkFormat);
then nothing happen. "Google" is just normal text.
Please help me insert clickable hyperlink to QTextEdit
.
Using QTextBrowser
is simpler (as suggested by another answer). However, if for some reason you want to use QTextEdit
, try to change the text interaction flags using setTextInteractionFlags()
.
I think you have to enable the Qt::LinksAccessibleByMouse
flag.
See Qt::TextInteractionFlag and QTextEdit::textInteractionFlags
To have clickable hyperlink in QTextEdit, you can use
QTextCharFormat::setAnchorHref
to set the link for some text
QWidget::mousePressEvent
to capture mouse press event
QTextEdit::anchorAt
to obtain the link
Here's the minimal working PyQt example,
import sys
from PyQt5.Qt import QDesktopServices, QUrl, QApplication, QColor, Qt
from PyQt5.QtWidgets import QTextEdit
class MyWidget(QTextEdit):
def mousePressEvent(self, e):
self.anchor = self.anchorAt(e.pos())
if self.anchor:
QApplication.setOverrideCursor(Qt.PointingHandCursor)
def mouseReleaseEvent(self, e):
if self.anchor:
QDesktopServices.openUrl(QUrl(self.anchor))
QApplication.setOverrideCursor(Qt.ArrowCursor)
self.anchor = None
app = QApplication(sys.argv)
editor = MyWidget()
cursor = editor.textCursor()
fmt = cursor.charFormat()
fmt.setForeground(QColor('blue'))
address = 'http://example.com'
fmt.setAnchor(True)
fmt.setAnchorHref(address)
fmt.setToolTip(address)
cursor.insertText("Hello world again", fmt)
editor.show()
app.exec_()
As far as I've tried, when using QTextEdit
+ Qt::LinksAccessibleByMouse
I'm able to click on links, but no action is taken (i.e., link is not open). The only action possible is to right-click on the link and select Copy Link Location
.
As mentioned, one option is using QTextBrowser
. In this case you have to set the QTextBrowser::openExternalLinks
property too, in order to open the link using the default browser, otherwise it will be open in the text-browser widget.
Another option, given you have a read-only text, is to use a QLabel
with rich format, and using the QLabel::linkActivated
signal to open the URL
label->setTextFormat(Qt::RichText);
QObject::connect(label, &QLabel::linkActivated, [](const QString & link) {
QDesktopServices::openUrl(link);
});
You can use QTextBrowser instead of QTextEdit if it read only text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With