QTextBrowser会试图自己打开链接,大部分时候这不是你想要的效果,所以要setOpenLinks(false)。之后捕获anchorClicked信号,然后调用ShellExecute函数用系统默认浏览器打开url。参考代码如下
===============================================
#include "testtextbrowser.h"
#include <QString>
#include <windows.h>
TestTextBrowser::TestTextBrowser(QWidget *parent, Qt::WFlags flags)
:
QMainWindow(parent, flags)
{
ui.setupUi(this);
ui.textBrowser->setOpenLinks(false);
connect(ui.textBrowser, SIGNAL(anchorClicked(const
QUrl&)),this, SLOT(anchorClickedSlot(const
QUrl&)));
ui.textBrowser->append(QString::fromLocal8Bit("<a
href =
\"http://www.sina.com.cn/\">新浪</a>"));
}
void TestTextBrowser::anchorClickedSlot(const QUrl& url)
{
ShellExecuteA(NULL, "open", url.toString().toStdString().c_str(),
"", "", SW_SHOW);
}
===============================================
#include "testtextbrowser.h"
#include <QString>
#include <windows.h>
TestTextBrowser::TestTextBrowser(QWidget *parent, Qt::WFlags flags)
{
}
void TestTextBrowser::anchorClickedSlot(const QUrl& url)
{
}