QT 4.8.6 TextBrowser Displaying Hyperlinks That Runs Functions

49 views Asked by At

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

There are 1 answers

2
Pamputt On

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

Something like that:

connect(ui.textBrowser, &QTextBrowser::anchorClicked,
        this, &dlgMessages::handleLinkClicked);