QT,QLabel添加超链接

1.方法1:使用信号槽绑定方式

//设置超链接并绑定信号槽
QLabel *linkLabel = new QLabel();
linkLabel->setText("<a href="http://www.cnblog.com/fron_csl">linkLabelTest");
connect(linkLabel, SIGNAL(linkActivated(QString)), this, SLOT(openUrl(QString)));

//槽函数实现
void testWidget::openUrl(QString url)
{
QDesktopServices::openUrl(QUrl(url));
// //若是文件路径,则需使用下面的打开方式,具体可参见QUrl帮助文档
// QDesktopServices::openUrl(QUrl("file:///" + url, QUrl::TolerantMode));
}

2.方法2:通过设置QLabel属性实现超链接(此方法不需要绑定信号槽,比较简单)
linkLabel->setOpenExternalLinks(true);
linkLabel->setText("<a href="http://www.cnblog.com/fron_csl">linkLabelTest");

QT是支持HTML的,所以呢,以下设置有效
1,设置超链接颜色
linkLabel->setText("<a style='color: green;' href="http://www.cnblog.com/fron_csl">linkLabel");

2,去掉超链接下面的下划线
linkLabel->setText("<style> a {text-decoration: none} </style> <a href="http://www.cnblog.com/fron_csl">linkLabel");

原文地址:https://www.cnblogs.com/amwuau/p/8000730.html