Text Element Reference

The Text item allows you to add formatted text to a scene.

[Inherits Item]

This element was introduced in Qt 4.7.

Detailed Description

The Text item allows you to add formatted text to a scene.

Text items can display both plain and rich text. For example, red text with a specific font and size can be defined like this:

  1. Text  {
  2.     text: "Hello World!"
  3.     font.family: "Helvetica"
  4.     font.pointSize: 24
  5.     color: "red"
  6. }

Rich text is defined using HTML-style markup:

  1. Text  {
  2.     text: "<b>Hello</b> <i>World!</i>"
  3. }

If height and width are not explicitly set, Text will attempt to determine how much room is needed and set it accordingly. Unless wrapMode is set, it will always prefer width to height (all text will be placed on a single line).

The elide property can alternatively be used to fit a single line of plain text to a set width.

Note that the Supported HTML Subset is limited. Also, if the text contains HTML img tags that load remote images, the text is reloaded.

Text provides read-only text. For editable text, see TextEdit.

Properties

Signal Handlers

Property Documentation

This property holds whether the text is clipped.

Note that if the text does not fit in the bounding rectangle it will be abruptly chopped.

If you want to display potentially long text in a limited space, you probably want to use elide instead.

The text color.

An example of green text defined using hexadecimal notation:

  1.                 Text  {
  2.     color: "#00FF00"
  3.     text: "green text"
  4. }

An example of steel blue text defined using an SVG color name:

  1.                 Text  {
  2.     color: "steelblue"
  3.     text: "blue text"
  4. }

Set this property to elide parts of the text fit to the Text item's width. The text will only elide if an explicit width has been set.

This property cannot be used with rich text.

Eliding can be:

  • Text.ElideNone - the default
  • Text.ElideLeft
  • Text.ElideMiddle
  • Text.ElideRight

If this property is set to Text.ElideRight, it can be used with multiline text. The text will only elide if maximumLineCount has been set.

If the text is a multi-length string, and the mode is not Text.ElideNone, the first string that fits will be used, otherwise the last will be elided.

Multi-length strings are ordered from longest to shortest, separated by the Unicode "String Terminator" character U009C (write this in QML with "\u009C" or "\x9C").

Sets whether the font weight is bold.

Sets the capitalization for the text.

  • Font.MixedCase - This is the normal text rendering option where no capitalization change is applied.
  • Font.AllUppercase - This alters the text to be rendered in all uppercase type.
  • Font.AllLowercase - This alters the text to be rendered in all lowercase type.
  • Font.SmallCaps - This alters the text to be rendered in small-caps type.
  • Font.Capitalize - This alters the text to be rendered with the first character of each word as an uppercase character.
  1.                 Text  { text: "Hello"; font.capitalization: Font.AllLowercase }

Sets the family name of the font.

The family name is case insensitive and may optionally include a foundry name, e.g. "Helvetica [Cronyx]". If the family is available from more than one foundry and the foundry isn't specified, an arbitrary foundry is chosen. If the family isn't available a family will be set using the font matching algorithm.

  • font.italic : bool

Sets whether the font has an italic style.

  • font.letterSpacing : real

Sets the letter spacing for the font.

Letter spacing changes the default spacing between individual letters in the font. A positive value increases the letter spacing by the corresponding pixels; a negative value decreases the spacing.

  • font.pixelSize : int

Sets the font size in pixels.

Using this function makes the font device dependent. Use pointSize to set the size of the font in a device independent manner.

  • font.pointSize : real

Sets the font size in points. The point size must be greater than zero.

  • font.strikeout : bool

Sets whether the font has a strikeout style.

  • font.underline : bool

Sets whether the text is underlined.

Sets the font's weight.

The weight can be one of:

  • Font.Light
  • Font.Normal - the default
  • Font.DemiBold
  • Font.Bold
  • Font.Black
  1.                 Text  { text: "Hello"; font.weight: Font.DemiBold }
  • font.wordSpacing : real

Sets the word spacing for the font.

Word spacing changes the default spacing between individual words. A positive value increases the word spacing by a corresponding amount of pixels, while a negative value decreases the inter-word spacing accordingly.

Sets the horizontal and vertical alignment of the text within the Text items width and height. By default, the text is vertically aligned to the top. Horizontal alignment follows the natural alignment of the text, for example text that is read from left to right will be aligned to the left.

The valid values for horizontalAlignment are Text.AlignLeft, Text.AlignRight, Text.AlignHCenter and Text.AlignJustify. The valid values for verticalAlignment are Text.AlignTop, Text.AlignBottom and Text.AlignVCenter.

Note that for a single line of text, the size of the text is the area of the text. In this common case, all alignments are equivalent. If you want the text to be, say, centered in its parent, then you will need to either modify the Item::anchors, or set horizontalAlignment to Text.AlignHCenter and bind the width to that of the parent.

When using the attached property LayoutMirroring::enabled to mirror application layouts, the horizontal alignment of text will also be mirrored. However, the property horizontalAlignment will remain unchanged. To query the effective horizontal alignment of Text, use the property LayoutMirroring::enabled.

  • lineCount : int

Returns the number of lines visible in the text item.

This property is not supported for rich text.

This property group was introduced in QtQuick 1.1.

See also maximumLineCount.

Sets the line height for the text. The value can be in pixels or a multiplier depending on lineHeightMode.

The default value is a multiplier of 1.0. The line height must be a positive value.

This property group was introduced in QtQuick 1.1.

This property determines how the line height is specified. The possible values are:

  • Text.ProportionalHeight (default) - this sets the spacing proportional to the line (as a multiplier). For example, set to 2 for double spacing.
  • Text.FixedHeight - this sets the line height to a fixed line height (in pixels).
  • maximumLineCount : int

Set this property to limit the number of lines that the text item will show. If elide is set to Text.ElideRight, the text will be elided appropriately. By default, this is the value of the largest possible integer.

This property is not supported for rich text.

This property group was introduced in QtQuick 1.1.

See also lineCount and elide.

  • paintedHeight : real

Returns the height of the text, including height past the height which is covered due to there being more text than fits in the set height.

  • paintedWidth : real

Returns the width of the text, including width past the width which is covered due to insufficient wrapping if WrapMode is set.

This property holds whether the text is smoothly scaled or transformed.

Smooth filtering gives better visual quality, but is slower. If the item is displayed at its natural size, this property has no visual or performance effect.

Note: Generally scaling artifacts are only visible if the item is stationary on the screen. A common pattern when animating an item is to disable smooth filtering at the beginning of the animation and reenable it at the conclusion.

Set an additional text style.

Supported text styles are:

  • Text.Normal - the default
  • Text.Outline
  • Text.Raised
  • Text.Sunken
  1.                 Row  {
  2.     Text  { font.pointSize: 24; text: "Normal" }
  3.     Text  { font.pointSize: 24; text: "Raised"; style: Text.Raised; styleColor: "#AAAAAA" }
  4.     Text  { font.pointSize: 24; text: "Outline";style: Text.Outline; styleColor: "red" }
  5.     Text  { font.pointSize: 24; text: "Sunken"; style: Text.Sunken; styleColor: "#AAAAAA" }
  6. }

Defines the secondary color used by text styles.

styleColor is used as the outline color for outlined text, and as the shadow color for raised or sunken text. If no style has been set, it is not used at all.

  1.                 Text  { font.pointSize: 18; text: "hello"; style: Text.Raised; styleColor: "gray" }

See also style.

The text to display. Text supports both plain and rich text strings.

The item will try to automatically determine whether the text should be treated as rich text. This determination is made using Qt::mightBeRichText().

The way the text property should be displayed.

Supported text formats are:

  • Text.AutoText (default)
  • Text.PlainText
  • Text.RichText
  • Text.StyledText

If the text format is Text.AutoText the text element will automatically determine whether the text should be treated as rich text. This determination is made using Qt::mightBeRichText().

Text.StyledText is an optimized format supporting some basic text styling markup, in the style of html 3.2:

  1. <font size="4" color="#ff0000">font size and color</font>
  2. <b>bold</b>
  3. <i>italic</i>
  4. <br>
  5. &gt; &lt; &amp;

Text.StyledText parser is strict, requiring tags to be correctly nested.

  1.                                     Column  {
  2.     Text  {
  3.         font.pointSize: 24
  4.         text: "<b>Hello</b> <i>World!</i>"
  5.     }
  6.     Text  {
  7.         font.pointSize: 24
  8.         textFormat: Text.RichText
  9.         text: "<b>Hello</b> <i>World!</i>"
  10.     }
  11.     Text  {
  12.         font.pointSize: 24
  13.         textFormat: Text.PlainText
  14.         text: "<b>Hello</b> <i>World!</i>"
  15.     }
  16. }

Returns true if the text has been truncated due to maximumLineCount or elide.

This property is not supported for rich text.

This property group was introduced in QtQuick 1.1.

See also maximumLineCount and elide.

Set this property to wrap the text to the Text item's width. The text will only wrap if an explicit width has been set. wrapMode can be one of:

  • Text.NoWrap (default) - no wrapping will be performed. If the text contains insufficient newlines, then paintedWidth will exceed a set width.
  • Text.WordWrap - wrapping is done on word boundaries only. If a word is too long, paintedWidth will exceed a set width.
  • Text.WrapAnywhere - wrapping is done at any point on a line, even if it occurs in the middle of a word.
  • Text.Wrap - if possible, wrapping occurs at a word boundary; otherwise it will occur at the appropriate point on the line, even in the middle of a word.

Signal Handler Documentation

  • Text::onLinkActivated ( string link )

This handler is called when the user clicks on a link embedded in the text. The link must be in rich text or HTML format and the link string provides access to the particular link.

  1.     Text  {
  2.             textFormat: Text.RichText
  3.             text: "The main website is at <a href=\"http://qt.nokia.com\">Nokia Qt DF</a>."
  4.             onLinkActivated: console.log(link + " link activated")
  5.     }

The example code will display the text "The main website is at Nokia Qt DF."

Clicking on the highlighted link will output http://qt.nokia.com link activated to the console.

Notes provided by the Qt Community
Cool Hack
  • 0

Votes: 0

Coverage: Qt library 4.7, 4.8

Picture of Alicemirror Alicemirror

Lab Rat
2 notes

lineHeightMode seems read-only: #QTBUG-1792 and Workaround

Due to the QTBUG-1792 [bugreports.qt.nokia.com] lineHeightMode and other properties that are expected to be settable appear as read-only. The result is that it is impossible to setup these properties in the Text element.

Workaround
Maybe helpful until the bug remain unsolved (actually Qt 4.7.4 + Qt Creator 2.3.0 + Qt SDK 1.1.3) to know the following workaround:

Setting a signal in the Text element i.e. ComponentOnCompleted the same property can be assigned and has effect. Thus the workaround for this property assignement maybe as in the following example:

  1. Text {
  2.     id: myText
  3.     text: "TextLine"
  4.     font.pixelSize: 12
  5.  
  6.     ComponentOnCompleted: {
  7.         lineHeightMode = Text.FixedSize // setting a pixel value
  8.         lineHeight = 18
  9.     }
  10. }

[Revisions]