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)
{
}
本文详细介绍了如何在Qt应用程序中使用QTextBrowser组件,通过设置openLinks属性为false来防止其自动打开链接,并通过捕获anchorClicked信号来实现自定义的链接处理逻辑,例如使用系统默认浏览器打开链接。
2226

被折叠的 条评论
为什么被折叠?



