QT 實現QLabel上的超鏈接
一個簡單的例子:
Widget::Widget(QWidget *parent)
: QWidget(parent){
QLabel* label = new QLabel("<a href = 要跳轉到的鏈接地址>顯示的內容</a>",this);
label->setFixedSize(200,100);
label->move(10,10);
label->show();
connect(label,SIGNAL(linkActivated(QString)),this,SLOT(openUrl(QString))); //在.h裏面定義一個槽private slots: void openUrl(QString url);
}
void Widget::openUrl(QString url)
{
QDesktopServices::openUrl(QUrl(url));
}
設置鏈接顏色:
ui.labView->setText(QString::fromLocal8Bit("<a
style='color: green;' href = www.doshow.com> 查看</a>"));
原文鏈接:http://hi.baidu.com/tibelf/item/2f27a733ae3ab9f2a984289f
感謝原作者的分享
方法二:
試了一下在QT中真是非常方便地就實現了,只要幾條語句
QLabel*openfilelabel=newQLabel(this);
//這句很關鍵啊,否則就只能通過linkActivated信號,連接到自定義槽函數中打開了
openfilelabel->setOpenExternalLinks(true);
openfilelabel->setText(tr("<a href=\"http://www.csdn.net/\">打開CSDN"));
openfilelabel.show();
就搞定了
原文鏈接:http://blog.csdn.net/menglongbor/article/details/7734308