Text QML Type▲
-
Import Statement: import QtQuick 2.13
-
Inherited By:: Label
-
Inherits: Item
-
Group: Text is part of qtquick-visual
Detailed Description▲
Text items can display both plain and rich text. For example, red text with a specific font and size can be defined like this:
Text
{
text
:
"Hello World!"
font.family
:
"Helvetica"
font.pointSize
:
24
color
:
"red"
}
Rich text is defined using HTML-style markup:
Text
{
text
:
"<b>Hello</b> <i>World!</i>"
}

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.
See Also▲
See also Fonts example
Property Documentation▲
effectiveHorizontalAlignment : enumeration▲
horizontalAlignment : enumeration
verticalAlignment : enumeration
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 read-only property effectiveHorizontalAlignment.
[since 5.6] bottomPadding : real▲
[since 5.6] leftPadding : real
[since 5.6] padding : real
[since 5.6] rightPadding : real
[since 5.6] topPadding : real
These properties hold the padding around the content. This space is reserved in addition to the contentWidth and contentHeight.
This QML property was introduced in Qt 5.6.
[since 5.10] advance : size▲
The distance, in pixels, from the baseline origin of the first character of the text item, to the baseline origin of the first character in a text item occurring directly after this one in a text flow.
Note that the advance can be negative if the text flows from the right to the left.
This property was introduced in Qt 5.10.
antialiasing : bool▲
Used to decide if the Text should use antialiasing or not. Only Text with renderType of Text.NativeRendering can disable antialiasing.
The default is true.
baseUrl : url▲
This property specifies a base URL which is used to resolve relative URLs within the text.
Urls are resolved to be within the same directory as the target of the base URL meaning any portion of the path after the last '/' will be ignored.
Base URL |
Relative URL |
Resolved URL |
---|---|---|
http://qt-project.org/ |
images/logo.png |
http://qt-project.org/images/logo.png |
http://qt-project.org/index.html |
images/logo.png |
http://qt-project.org/images/logo.png |
http://qt-project.org/content |
images/logo.png |
http://qt-project.org/content/images/logo.png |
http://qt-project.org/content/ |
images/logo.png |
http://qt-project.org/content/images/logo.png |
http://qt-project.org/content/index.html |
images/logo.png |
http://qt-project.org/content/images/logo.png |
http://qt-project.org/content/index.html |
../images/logo.png |
http://qt-project.org/images/logo.png |
http://qt-project.org/content/index.html |
/images/logo.png |
http://qt-project.org/images/logo.png |
The default value is the url of the QML file instantiating the Text item.
clip : bool▲
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.
color : color▲
The text color.
An example of green text defined using hexadecimal notation:
Text
{
color
:
"#00FF00"
text
:
"green text"
}
An example of steel blue text defined using an SVG color name:
Text
{
color
:
"steelblue"
text
:
"blue text"
}
contentHeight : 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.
contentWidth : real▲
Returns the width of the text, including width past the width which is covered due to insufficient wrapping if WrapMode is set.
elide : enumeration▲
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 wrapped text. The text will only elide if maximumLineCount, or height has been set. If both maximumLineCount and height are set, maximumLineCount will apply unless the lines do not fit in the height allowed.
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").
font.bold : bool▲
Sets whether the font weight is bold.
font.capitalization : enumeration▲
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.
Text
{
text
:
"Hello"
; font.capitalization
:
Font.AllLowercase }
font.family : string▲
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.
[since 5.8] font.hintingPreference : enumeration▲
Sets the preferred hinting on the text. This is a hint to the underlying text rendering system to use a certain level of hinting, and has varying support across platforms. See the table in the documentation for QFont::HintingPreference for more details.
This property only has an effect when used together with render type Text.NativeRendering.
Font.PreferDefaultHinting - Use the default hinting level for the target platform.
Font.PreferNoHinting - If possible, render text without hinting the outlines of the glyphs. The text layout will be typographically accurate, using the same metrics as are used e.g. when printing.
Font.PreferVerticalHinting - If possible, render text with no horizontal hinting, but align glyphs to the pixel grid in the vertical direction. The text will appear crisper on displays where the density is too low to give an accurate rendering of the glyphs. But since the horizontal metrics of the glyphs are unhinted, the text's layout will be scalable to higher density devices (such as printers) without impacting details such as line breaks.
Font.PreferFullHinting - If possible, render text with hinting in both horizontal and vertical directions. The text will be altered to optimize legibility on the target device, but since the metrics will depend on the target size of the text, the positions of glyphs, line breaks, and other typographical detail will not scale, meaning that a text layout may look different on devices with different pixel densities.
Text
{
text
:
"Hello"
; renderType
:
Text.NativeRendering; font.hintingPreference
:
Font.PreferVerticalHinting }
This property was introduced in Qt 5.8.
font.italic : bool▲
Sets whether the font has an italic style.
[since 5.10] font.kerning : bool▲
Enables or disables the kerning OpenType feature when shaping the text. Disabling this may improve performance when creating or changing the text, at the expense of some cosmetic features. The default value is true.
Text
{
text
:
"OATS FLAVOUR WAY"
; font.kerning
:
false
}
This property was introduced in Qt 5.10.
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.
[since 5.10] font.preferShaping : bool▲
Sometimes, a font will apply complex rules to a set of characters in order to display them correctly. In some writing systems, such as Brahmic scripts, this is required in order for the text to be legible, but in e.g. Latin script, it is merely a cosmetic feature. Setting the preferShaping property to false will disable all such features when they are not required, which will improve performance in most cases.
The default value is true.
Text
{
text
:
"Some text"
; font.preferShaping
:
false
}
This property was introduced in Qt 5.10.
font.strikeout : bool▲
Sets whether the font has a strikeout style.
[since 5.6] font.styleName : string▲
Sets the style name of the font.
The style name is case insensitive. If set, the font will be matched against style name instead of the font properties font.weight, font.bold and font.italic.
This property was introduced in Qt 5.6.
font.underline : bool▲
Sets whether the text is underlined.
font.weight : enumeration▲
Sets the font's weight.
The weight can be one of:
-
Font.Thin
-
Font.Light
-
Font.ExtraLight
-
Font.Normal - the default
-
Font.Medium
-
Font.DemiBold
-
Font.Bold
-
Font.ExtraBold
-
Font.Black
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.
[since 5.9] fontInfo.bold : bool▲
The bold state of the font info that has been resolved for the current font and fontSizeMode. This is true if the weight of the resolved font is bold or higher.
This property was introduced in Qt 5.9.
[since 5.9] fontInfo.family : string▲
The family name of the font that has been resolved for the current font and fontSizeMode.
This property was introduced in Qt 5.9.
[since 5.9] fontInfo.italic : bool▲
The italic state of the font info that has been resolved for the current font and fontSizeMode.
This property was introduced in Qt 5.9.
[since 5.9] fontInfo.pixelSize : string▲
The pixel size of the font info that has been resolved for the current font and fontSizeMode.
This property was introduced in Qt 5.9.
[since 5.9] fontInfo.pointSize : real▲
The pointSize of the font info that has been resolved for the current font and fontSizeMode.
This property was introduced in Qt 5.9.
[since 5.9] fontInfo.styleName : string▲
The style name of the font info that has been resolved for the current font and fontSizeMode.
This property was introduced in Qt 5.9.
[since 5.9] fontInfo.weight : int▲
The weight of the font info that has been resolved for the current font and fontSizeMode.
This property was introduced in Qt 5.9.
fontSizeMode : enumeration▲
This property specifies how the font size of the displayed text is determined. The possible values are:
-
Text.FixedSize (default) - The size specified by font.pixelSize or font.pointSize is used.
-
Text.HorizontalFit - The largest size up to the size specified that fits within the width of the item without wrapping is used.
-
Text.VerticalFit - The largest size up to the size specified that fits the height of the item is used.
-
Text.Fit - The largest size up to the size specified that fits within the width and height of the item is used.
The font size of fitted text has a minimum bound specified by the minimumPointSize or minimumPixelSize property and maximum bound specified by either the font.pointSize or font.pixelSize properties.
Text
{
text
:
"Hello"
; fontSizeMode
:
Text.Fit; minimumPixelSize
:
10
; font.pixelSize
:
72
}
If the text does not fit within the item bounds with the minimum font size the text will be elided as per the elide property.
[since 5.2] hoveredLink : string▲
This property contains the link string when the user hovers a link embedded in the text. The link must be in rich text or HTML format and the hoveredLink string provides access to the particular link.
This property was introduced in Qt 5.2.
See Also▲
See also linkHovered, linkAt()
lineCount : int▲
Returns the number of lines visible in the text item.
This property is not supported for rich text.
See Also▲
See also maximumLineCount
lineHeight : real▲
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.