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");