开发者

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.


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜