目录
效果图
源码
解析
效果图
源码
有两种方法可以实现,方法一是直接设置setOpenExternalLinks(true);方法二是使用信号槽的方式。
//头文件#include <QLabel>#include <QVBoxLayout>#include <QString>#include <QUrl>#include <QDebug>#include <QDesktopServices>//以下为源码——————————————————————————————————————————————————————————————————————————————this->resize(500,500);this->setWindowTitle("linkLabelTest");//方法一QLabel *linkLabel1 = new QLabel(this);linkLabel1->setOpenExternalLinks(true);//设置为true才能打开网页linkLabel1->setText("<a style='color: green; text-decoration: none' href = /wmcy123?type=lately>点我试试");linkLabel1->setAlignment(Qt::AlignCenter);//文字居中//方法二QLabel *linkLabel2 = new QLabel(this);linkLabel2->setText("<a style='color: red; text-decoration: none' href = /wmcy123?type=lately>点我试试");linkLabel2->setAlignment(Qt::AlignCenter);//文字居中//连接信号槽connect(linkLabel2, &QLabel::linkActivated, [=](QString url){QDesktopServices::openUrl(QUrl(url));});//使用垂直布局QVBoxLayout *layout = new QVBoxLayout(this);layout->addWidget(linkLabel1);layout->addWidget(linkLabel2);
解析
红色框框为所要跳转的网页链接,黑色框框为QLabel上面显示的内容,color: red 为设置字体颜色,text-decoration: none 为设置字体没有下划线。