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:
- Let the class that needs to open a file have a slot
foo
that takes aconst QUrl&
. foo
can useQUrl::toLocalFile
to construct aQFile
and open it.- Connect your
QTextBrowser
'sanchorClicked
signal tofoo
.
精彩评论