支持的 HTML 子集

Describes the support for HTML markup in text widgets.

Qt’s text widgets are able to display rich text, specified using a subset of HTML 4 markup. Widgets that use QTextDocument ,譬如 QLabel and QTextEdit ,能够显示以此方式指定的富文本。

在文本 Widget 中使用 HTML 标记

Widgets automatically detect HTML markup and display rich text accordingly. For example, setting a label’s text property with the string "<b>Hello</b> <i>Qt!</i>" will result in the label displaying text like this: Hello Qt!

When HTML markup is used for text, Qt follows the rules defined by the HTML 4 specification. This includes default properties for text layout, such as the direction of the text flow (left-to-right) which can be changed by applying the dir :ref:` <Supported-HTML-Subset>` attribute to blocks of text.

支持的 Tag

The following table lists the HTML tags supported by Qt’s rich text engine:

Tag

描述

Comment

a

锚点或链接

支持 href and name 属性。

address

Address

b

Bold

big

Larger font

blockquote

Indented paragraph

body

Document body

支持 bgcolor attribute, which can be a Qt color name #RRGGBB color specification.

br

Line break

center

Centered paragraph

cite

Inline citation

如同 i .

code

Code

如同 tt .

dd

Definition data

dfn

Definition

如同 i .

div

Document division

支持标准 块属性 .

dl

Definition list

支持标准 块属性 .

dt

Definition term

支持标准 块属性 .

em

Emphasized

如同 i .

font

Font size, family, and/or color

支持下列属性: size , face ,和 color (Qt color names or #RRGGBB ).

h1

Level 1 heading

支持标准 块属性 .

h2

Level 2 heading

支持标准 块属性 .

h3

Level 3 heading

支持标准 块属性 .

h4

Level 4 heading

支持标准 块属性 .

h5

Level 5 heading

支持标准 块属性 .

h6

Level 6 heading

支持标准 块属性 .

head

文档 Header 头

hr

Horizontal line

支持 width attribute, which can be specified as an absolute or relative ( % ) value.

html

HTML 文档

i

Italic

img

图像

支持 src , source (for Qt 3 compatibility), width ,和 height 属性。

kbd

User-entered text

meta

Meta-information

If a text encoding is specified using the meta tag, it is picked up by . Likewise, if an encoding is specified to toHtml() , the encoding is stored using a meta tag, for example: <meta http-equiv="Content-Type" content="text/html; charset=EUC-JP" />

li

List item

nobr

Non-breakable text

ol

Ordered list

支持标准 列表属性 .

p

Paragraph

Left-aligned by default. Supports the standard 块属性 .

pre

Preformated text

qt

Qt rich-text document

同义词为 html . Provided for compatibility with earlier versions of Qt.

s

Strikethrough

samp

Sample code

如同 tt .

small

Small font

span

Grouped elements

strong

Strong

如同 b .

sub

Subscript

sup

Superscript

table

Table

支持下列属性: border , bgcolor (Qt color names or #RRGGBB ), cellspacing , cellpadding , width (absolute or relative), and height .

tbody

表本体

什么都不做。

td

表数据单元格

支持标准 表单元格属性 .

tfoot

Table footer

什么都不做。

th

Table header cell

支持标准 表单元格属性 .

thead

Table header

thead tag is specified, it is used when printing tables that span multiple pages.

title

文档标题

The value specified using the title tag is available through metaInformation() .

tr

Table row

支持 bgcolor attribute, which can be a Qt color name #RRGGBB color specification.

tt

Typewrite font

u

Underlined

ul

Unordered list

支持标准 列表属性 .

var

Variable

如同 i .

块属性

The following attributes are supported by the div , dl , dt , h1 , h2 , h3 , h4 , h5 , h6 , p tags:

  • align ( left , right , center , justify )

  • dir ( ltr , rtl )

列表属性

The following attribute is supported by the ol and ul tags:

  • type ( 1 , a , A , square , disc , circle )

表单元格属性

The following attributes are supported by the td and th tags:

  • width (absolute, relative, or no-value)

  • bgcolor (Qt color names or #RRGGBB )

  • colspan

  • rowspan

  • align ( left , right , center , justify )

  • valign ( top , middle , bottom )

CSS 特性

The following table lists the CSS properties supported by Qt’s rich text engine:

Qt-specific CSS properties

Besides the standard CSS properties listed earlier, the following Qt-specific properties can also be used to style a text block:

特性

描述

-qt-block-indent

<number>

Indents the text block by the specified no. spaces.

-qt-list-indent

<number>

Indents the list items by the specified no. of spaces.

-qt-list-number-prefix

<string>

Prefixes the given string to list number in an HTML ordered list.

-qt-list-number-suffix

<string>

Suffixes the given string to list number in an HTML ordered list.

-qt-paragraph-type

empty

Hides the text block.

-qt-table-type

root | frame

root renders the text blocks inline without borders and indentation, whereas frame renders them on a new line with a frame around.

-qt-user-state

<number>

Adds it as user data for the text block.

支持的 CSS 选择器

All CSS 2.1 selector classes are supported except pseudo-class selectors such as :first-child , :visited and :hover .