Qt对Html富文本支持的控件以及QLabel两种打开超链接的方式



原创文章,欢迎转载。转载请注明:转载自 祥的博客

原文链接:https://blog.csdn.net/humanking7/article/details/80685893



1.说明

Qt的文本窗体部件能够显示富文本,使用HTML4 标记。能够以这种方式显示富文本的窗体控件有: QTextDocument, 以及 QLabel and QTextEdit

Qt’s text widgets are able to display rich text, specified using a subset of HTML4 markup. Widgets that use QTextDocument, such as QLabel and QTextEdit, are able to display rich text specified in this way.

2.演示

pic1

2.1. 代码

#include <QtWidgets/QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QString str;
    str = QString("<h1><i>Hello</i> <font color = red>Qt!</font></h1><h2>My CSDN blog: <a href=\"https://blog.csdn.net/humanking7\">https://blog.csdn.net/humanking7 </a></h2>");
    QLabel *lab = new QLabel(str);
    lab->setOpenExternalLinks(true);//如果没有这句,就只能通过linkActivated信号,连接到自定义槽函数中打开      
    lab->setWindowTitle("Test Html support");
    lab->show();

    return a.exec();
}

2.2. 关于打开超链接的两种方式说明

  1. 简单方式,用Qt自带的setOpenExternalLinks(true)函数进行设置;
  2. 用通过linkActivated信号,连接到自定义槽函数中打开超链接。

用第二种方式实现超链接[重新继承了一个QDialog类,在里面进行操作]:

pic2

文件1. main.cpp

#include <QtWidgets/QApplication>
#include "dlgShow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    dlgShow* w = new dlgShow();
    w->show();
    return a.exec();
}

文件2. dlgshow.h


#ifndef DLGSHOW_H
#define DLGSHOW_H

#include <QDialog>
#include <QLabel>
class dlgShow : public QDialog
{
    Q_OBJECT

public:
    dlgShow(QWidget *parent = 0);
    ~dlgShow();

protected slots:
    void openURL(QString url);
};

#endif // DLGSHOW_H

文件3. dlgshow.cpp

#include <QUrl>
#include <QDesktopServices>
#include "dlgshow.h"

dlgShow::dlgShow(QWidget *parent)
    : QDialog(parent)
{   
    setWindowTitle("Dlg:Test Html support");//设置标题
    QString str;
    str = QString("<h1><i>Hello</i> <font color = red>Qt!</font></h1><h2>My CSDN blog: <a href=\"https://blog.csdn.net/humanking7\">https://blog.csdn.net/humanking7 </a></h2>");
    QLabel *lab = new QLabel(str, this);//添加label
    lab->show();
    //绑定超链接与信号槽
    connect(lab, SIGNAL(linkActivated(QString)), this, SLOT(openURL(QString)));

}

void dlgShow::openURL(QString url)
{//槽函数
    //打开对应url的网址
    QDesktopServices::openUrl(QUrl(url));
}

dlgShow::~dlgShow()
{

}

3.支持的标签 Tags

下表列出了Qt富文本引擎支持的Html标签:

TagDescriptionComment
aAnchor or linkSupports the href and name attributes.
addressAddress
bBold
bigLarger font
blockquoteIndented paragraph
bodyDocument bodySupports the bgcolor attribute, which can be a Qt color name or a #RRGGBB color specification.
brLine break
centerCentered paragraph
citeInline citationSame as i.
codeCodeSame as tt.
ddDefinition data
dfnDefinitionSame as i.
divDocument divisionSupports the standard block attributes.
dlDefinition listSupports the standard block attributes.
dtDefinition termSupports the standard block attributes.
emEmphasizedSame as i.
fontFont size, family, and/or colorSupports the following attributes: size, face, and color (Qt color names or #RRGGBB).
h1Level 1 headingSupports the standard block attributes.
h2Level 2 headingSupports the standard block attributes.
h3Level 3 headingSupports the standard block attributes.
h4Level 4 headingSupports the standard block attributes.
h5Level 5 headingSupports the standard block attributes.
h6Level 6 headingSupports the standard block attributes.
headDocument header
hrHorizontal lineSupports the width attribute, which can be specified as an absolute or relative (%) value.
htmlHTML document
iItalic
imgImageSupports the src, source (for Qt 3 compatibility), width, and height attributes.
kbdUser-entered text
metaMeta-informationIf a text encoding is specified using the meta tag, it is picked up by Qt::codecForHtml(). Likewise, if an encoding is specified to QTextDocument::toHtml(), the encoding is stored using a meta tag, for example:
liList item
nobrNon-breakable text
olOrdered listSupports the standard list attributes.
pParagraphLeft-aligned by default. Supports the standard block attributes.
prePreformated text
qtQt rich-text documentSynonym for html. Provided for compatibility with earlier versions of Qt.
sStrikethrough
sampSample codeSame as tt.
smallSmall font
spanGrouped elements
strongStrongSame as b.
subSubscript
supSuperscript
tableTableSupports the following attributes: border, bgcolor (Qt color names or #RRGGBB), cellspacing, cellpadding, width (absolute or relative), and height.
tbodyTable bodyDoes nothing.
tdTable data cellSupports the standard table cell attributes.
tfootTable footerDoes nothing.
thTable header cellSupports the standard table cell attributes.
theadTable headerIf the thead tag is specified, it is used when printing tables that span multiple pages.
titleDocument titleThe value specified using the title tag is available through QTextDocument::metaInformation().
trTable rowSupports the bgcolor attribute, which can be a Qt color name or a #RRGGBB color specification.
ttTypewrite font
uUnderlined
ulUnordered listSupports the standard list attributes.
varVariableSame as i.

4. 对CSS的支持

下表列出了Qt富文本引擎支持的css

PropertyValuesDescription
background-colorBackground color for elements
background-imageBackground image for elements
colorText foreground color
font-familyFont family name
font-size[ smallmedium
font-style[ normalitalic
font-weight[ normalbold
text-decorationnone[ underline
font[ [ <’font-style’>
text-indentpxFirst line text indentation in pixels
white-spacenormalpre
margin-toppxTop paragraph margin in pixels
margin-bottompxBottom paragraph margin in pixels
margin-leftpxLeft paragraph margin in pixels
margin-rightpxRight paragraph margin in pixels
padding-toppxTop table cell padding in pixels
padding-bottompxBottom table cell padding in pixels
padding-leftpxLeft table cell padding in pixels
padding-rightpxRight table cell padding in pixels
paddingpxShorthand for setting all the padding properties at once.
vertical-alignbaselinesub
border-colorBorder color for text tables.
border-stylenonedotted
background[ <’background-color’>
page-break-before[ autoalways ]
page-break-after[ autoalways ]
float[ leftright
text-transform[ uppercaselowercase ]
font-variantsmall-capsPerform the smallcaps transformation on the text prior to displaying it.
word-spacingpxSpecifies an alternate spacing between each word.

赞赏码New

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值