QLabel的文本顯示是支持html標籤的,使用html有時候能很便捷的實現一些效果。這裏記錄一下。
顯示超鏈接
ui->label->setText(tr("Learn more please visit: <a style=color:#32bc84; href = http://www.baidu.com>www.baidu.com</a>"));
標籤<a>用於顯示超鏈接的
點擊能夠跳轉需要添加信號槽:
connect(ui->label, &QLabel::linkActivated, this, [=](const QString& link){
QDesktopServices::openUrl(QUrl(link));
});
顯示效果:
一行文字顯示不同的字體大小和顏色
先看效果:
代碼:
ui->label->setText("<p style=\"color:red;font-size:16px;\"> hello <b style=\"color:black;font-size:22px;\">world</b></p>");
<b>標籤爲文本顯示,可使用css設置一些參數,<b>標籤爲粗體的文本顯示,如果不需要粗體可以<b>嵌套<b>,再外層再加一個<b>會換到另外一行