Supported HTML Subset | Qt GUI (original) (raw)

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

Using HTML Markup in Text Widgets

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 attribute to blocks of text.

The following table lists the HTML tags supported by Qt's rich text engine.

Note: The functionality implemented for tags listed below is a subset of the full HTML 4 specification. Not all attributes are supported, see comments for each tag.

Tag Description Comment
a Anchor or link Supports the href and name attributes.
address Address
b Bold
big Larger font
blockquote Indented paragraph
body Document body Supports the bgcolor attribute, which can be a Qt color name or a #RRGGBB color specification.
br Line break
center Centered paragraph
cite Inline citation Same as i.
code Code Same as tt.
dd Definition data
dfn Definition Same as i.
div Document division Supports the standard block attributes.
dl Definition list Supports the standard block attributes.
dt Definition term Supports the standard block attributes.
em Emphasized Same as i.
font Font size, family, and/or color Supports the following attributes: size, face, and color (Qt color names or #RRGGBB).
h1 Level 1 heading Supports the standard block attributes.
h2 Level 2 heading Supports the standard block attributes.
h3 Level 3 heading Supports the standard block attributes.
h4 Level 4 heading Supports the standard block attributes.
h5 Level 5 heading Supports the standard block attributes.
h6 Level 6 heading Supports the standard block attributes.
head Document header
hr Horizontal line Supports the width attribute, which can be specified as an absolute or relative (%) value.
html HTML document
i Italic
img Image Supports the src, source (for Qt 3 compatibility), width, and height attributes.
kbd User-entered text
meta Meta-information If 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:
li List item
nobr Non-breakable text
ol Ordered list Supports the standard list attributes.
p Paragraph Left-aligned by default. Supports the standard block attributes.
pre Preformated text
qt Qt rich-text document Synonym for html. Provided for compatibility with earlier versions of Qt.
s Strikethrough
samp Sample code Same as tt.
small Small font
span Grouped elements
strong Strong Same as b.
sub Subscript
sup Superscript
table Table Supports the following attributes: border, bgcolor (Qt color names or #RRGGBB), cellspacing, cellpadding, width (absolute or relative), and height.
tbody Table body Does nothing.
td Table data cell Supports the standard table cell attributes.
tfoot Table footer Does nothing.
th Table header cell Supports the standard table cell attributes.
thead Table header If the thead tag is specified, it is used when printing tables that span multiple pages.
title Document title The value specified using the title tag is available through QTextDocument::metaInformation().
tr Table row Supports the bgcolor attribute, which can be a Qt color name or a #RRGGBB color specification.
tt Typewrite font
u Underlined
ul Unordered list Supports the standard list attributes.
var Variable Same as i.

Block Attributes

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

List Attributes

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

Table Cell Attributes

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

CSS Properties

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

Property Values Description
background-color Background color for elements
background-image Background image for elements
color Text foreground color
font-family Font family name
font-size [ small | medium large x-large xx-large ] pt px Font size relative to the document font, or specified in points or pixels
font-style [ normal | italic oblique ]
font-weight [ normal | bold 100 200 300 400 500 600 700 800 900 ] Specifies the font weight used for text, where normal and bold are mapped to the corresponding QFont weights. Numeric values are 8 times the equivalent QFont weight values.
text-decoration none | [ underline overline line-through ] Additional text effects
font [ [ <'font-style'> | <'font-weight'> ]? <'font-size'> <'font-family'> ] Font shorthand property
text-indent px First line text indentation in pixels
white-space normal | pre nowrap pre-wrap Declares how whitespace in HTML is handled.
margin-top px Top paragraph margin in pixels
margin-bottom px Bottom paragraph margin in pixels
margin-left px Left paragraph margin in pixels
margin-right px Right paragraph margin in pixels
padding-top px Top table cell padding in pixels
padding-bottom px Bottom table cell padding in pixels
padding-left px Left table cell padding in pixels
padding-right px Right table cell padding in pixels
padding px Shorthand for setting all the padding properties at once.
vertical-align baseline | sub super middle top bottom Vertical text alignment. For vertical alignment in text table cells only middle, top, and bottom apply.
border-collapse collapse | separate Border Collapse mode for text tables. If set to collapse, cell-spacing will not be applied.
border-color Border color for text tables and table cells.
border-top-color Top border color for table cells.
border-bottom-color Bottom border color for table cells.
border-left-color Left border color for table cells.
border-right-color Right border color for table cells.
border-style none | dotted dashed dot-dash dot-dot-dash solid double groove ridge inset outset Border style for text tables and table cells.
border-top-style Top border style for table cells.
border-bottom-style Bottom border style for table cells.
border-left-style Left border style for table cells.
border-right-style Right border style for table cells.
border-width px Width of table or cell border
border-top-width px Top border width for table cells.
border-bottom-width px Bottom border width for table cells.
border-left-width px Left border width for table cells.
border-right-width px Right border width for table cells.
border-top px Shorthand for setting top border width, style and color
border-bottom px Shorthand for setting bottom border width, style and color
border-left px Shorthand for setting left border width, style and color
border-right px Shorthand for setting right border width, style and color
border-top px Shorthand for setting top border width, style and color
border-bottom px Shorthand for setting bottom border width, style and color
border px Shorthand for setting all four border's width, style and color
background [ <'background-color'> | <'background-image'> ] Background shorthand property
page-break-before [ auto | always ] Make it possible to enforce a page break before the paragraph/table
page-break-after [ auto | always ] Make it possible to enforce a page break after the paragraph/table
float [ left | right none ] Specifies where an image or a text will be placed in another element. Note that the float property is only supported for tables and images.
text-transform [ uppercase | lowercase ] Select the transformation that will be performed on the text prior to displaying it.
font-kerning [ normal | none ] Enables or disables kerning between text characters.
font-variant small-caps Perform the smallcaps transformation on the text prior to displaying it.
word-spacing px Specifies an alternate spacing between each word.
line-height [% | px pt cm] Specifies the height of a line. It can be one of the following: fixed line height in pixels, points, or centimeters. a percentage of the current font size.

Note: Certain CSS properties are only supported when applied to a block tag, such as <p>. Setting these properties on a <span>, for instance, will have no effect. The margin is one example of such a property. See QTextBlockFormat and QTextCharFormat for an overview of the formatting options available on blocks and spans in QTextDocument.

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:

Property Values Description
-qt-block-indent Indents the text block by the specified no. spaces.
-qt-list-indent Indents the list items by the specified no. of spaces.
-qt-list-number-prefix Prefixes the given string to list number in an HTML ordered list.
-qt-list-number-suffix 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 Adds it as user data for the text block.

Supported CSS Selectors

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