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
.
2
Answers
I just figured it out somehow with this line:
connect(ui.txt_error, SIGNAL(anchorClicked(const QUrl&)), this, SLOT(handleLinkClicked(const QUrl&)));
Now, I can run a function but still got
QTextBrowser: No document for helloLink
output. I guess program thinks I didin't handle the helloLink URL. How can I fix this?You should connect the QTextBrowser::anchorClicked signal to your
handleLinkClicked()
slot.Something like that: