admin管理员组

文章数量:1619183

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/\">新浪</a>"));
}

void TestTextBrowser::anchorClickedSlot(const QUrl& url)
{
       ShellExecuteA(NULL, "open", url.toString().toStdString().c_str(), "", "", SW_SHOW);  
}

本文标签: 如何使用浏览器链接系统QTextBrowser