0

I want to add a hyperlink to a textBrowser in order to open a dialog.

To be specific, I am using the textBrowser as a error messages window and if I click to a message I want to open a dialog. I made a hyperlink that opens website with ui.textBrowser.append("<a href=https://www.google.com>Google</a>") but as I said, I want the link to run a function in order to display a dialog.

I tried something like this:

void dlgMessages::on_btnerr_clicked()
{
    QString linkText = "<a href=\"helloLink\">HELLO</a>";
    ui.textBrowser->setOpenExternalLinks(false);
    ui.textBrowser->moveCursor(QTextCursor::Start);
    ui.textBrowser->append(linkText);
}

void dlgMessages::handleLinkClicked(const QUrl& url)
{
    if (url.toString() == "helloLink") {
        qDebug() << "Hello Guys";
    }
}

Those functions simply does append a hyperlink to the textBrowser and when user click the message and if the message's link is "helloLink", I expect an output that says "Hello Guys". But I got QTextBrowser: No document for helloLink.

1 Answers1

0

You should connect the QTextBrowser::anchorClicked signal to your handleLinkClicked() slot.

Something like that:

connect(ui.textBrowser, &QTextBrowser::anchorClicked,
        this, &dlgMessages::handleLinkClicked);
Pamputt
  • 173
  • 1
  • 11
  • This won't work for Qt-4.8.6. I was going to ask why the OP was still using that however when I think of it, I still have a production application that uses Qt-4.8.7 at work. We can't upgrade because of the time to port (estimated 6 months work) an additional third library dependency which the updated version that supports Qt5 has breaking changes. – drescherjm Jun 13 '23 at 14:02
  • Qt4(.8) has not been supported since December 2015. Continuing to develop with this version is _astonishing_... – Pamputt Jun 13 '23 at 14:31