CrazyCoder
CrazyCoder

Reputation: 2605

Open file from QTextBrowser

I am just trying to write a code to open file by clicking a link which is displayed in QTextBrowser. But file is not getting open instead it is displaying in QTextBrowser itself. Here is my code

void MainWindow::openTextEdit()
{
    QTextBrowser *p = new QTextBrowser();
    p->show();

    p->append("<a href = \"/home/winbros/Test.cpp\"> Link </a>");
    p->append("<a href = \"/home/winbros/Test.doc\"> Link </a>");
    p->append("<a href = \"/home/winbros/Test.xls\"> Link </a>");
    p->setOpenExternalLinks(true);
}

I am using QT creator. Guys please let me know to use anchor clicked in this sense.

Upvotes: 2

Views: 2223

Answers (1)

gspr
gspr

Reputation: 11227

It sounds like the anchorClicked signal could be useful to you. It contains the URL of the clicked link as a QUrl.

Edit: OP asks for a usage example. I don't have time for that right now, but here's roughly what I would do:

  1. Let the class that needs to open a file have a slot foo that takes a const QUrl&.
  2. foo can use QUrl::toLocalFile to construct a QFile and open it.
  3. Connect your QTextBrowser's anchorClicked signal to foo.

Upvotes: 2

Related Questions