TextEdit QML Type

  • Import Statement: import QtQuick 2.13

  • Inherited By:: TextArea

  • Inherits: Item

  • Group: TextEdit is part of qtquick-visual, qtquick-input

Detailed Description

The TextEdit item displays a block of editable, formatted text.

It can display both plain and rich text. For example:

 
Sélectionnez
TextEdit {
    width: 240
    text: "<b>Hello</b> <i>World!</i>"
    font.family: "Helvetica"
    font.pointSize: 20
    color: "blue"
    focus: true
}
Image non disponible

Setting focus to true enables the TextEdit item to receive keyboard focus.

Note that the TextEdit does not implement scrolling, following the cursor, or other behaviors specific to a look-and-feel. For example, to add flickable scrolling that follows the cursor:

 
Sélectionnez
Flickable {
     id: flick

     width: 300; height: 200;
     contentWidth: edit.paintedWidth
     contentHeight: edit.paintedHeight
     clip: true

     function ensureVisible(r)
     {
         if (contentX >= r.x)
             contentX = r.x;
         else if (contentX+width <= r.x+r.width)
             contentX = r.x+r.width-width;
         if (contentY >= r.y)
             contentY = r.y;
         else if (contentY+height <= r.y+r.height)
             contentY = r.y+r.height-height;
     }

     TextEdit {
         id: edit
         width: flick.width
         focus: true
         wrapMode: TextEdit.Wrap
         onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
     }
 }

A particular look-and-feel might use smooth scrolling (eg. using SmoothedAnimation), might have a visible scrollbar, or a scrollbar that fades in to show location, etc.

Clipboard support is provided by the cut(), copy(), and paste() functions, and the selection can be handled in a traditional "mouse" mechanism by setting selectByMouse, or handled completely from QML by manipulating selectionStart and selectionEnd, or using selectAll() or selectWord().

You can translate between cursor positions (characters from the start of the document) and pixel points using positionAt() and positionToRectangle().

See Also

See also Text, TextInput

Property Documentation

 

effectiveHorizontalAlignment : enumeration

horizontalAlignment : enumeration

verticalAlignment : enumeration

Sets the horizontal and vertical alignment of the text within the TextEdit item's width and height. By default, the text alignment follows the natural alignment of the text, for example text that is read from left to right will be aligned to the left.

Valid values for horizontalAlignment are:

Valid values for verticalAlignment are:

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 TextEdit, 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.

activeFocusOnPress : bool

Whether the TextEdit should gain active focus on a mouse press. By default this is set to true.

baseUrl : url

This property specifies a base URL which is used to resolve relative URLs within the text.

The default value is the url of the QML file instantiating the TextEdit item.

canPaste : bool

Returns true if the TextEdit is writable and the content of the clipboard is suitable for pasting into the TextEdit.

canRedo : bool

Returns true if the TextEdit is writable and there are undone operations that can be redone.

canUndo : bool

Returns true if the TextEdit is writable and there are previous operations that can be undone.

color : color

The text color.

 
Sélectionnez
// green text using hexadecimal notation
TextEdit { color: "#00FF00" }
 
Sélectionnez
// steelblue text using SVG color name
TextEdit { color: "steelblue" }

contentHeight : real

Returns the height of the text, including the height past the height that is covered if the text does not fit within the set height.

contentWidth : real

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

cursorDelegate : Component

The delegate for the cursor in the TextEdit.

If you set a cursorDelegate for a TextEdit, this delegate will be used for drawing the cursor instead of the standard cursor. An instance of the delegate will be created and managed by the text edit when a cursor is needed, and the x and y properties of delegate instance will be set so as to be one pixel before the top left of the current character.

Note that the root item of the delegate component must be a QQuickItem or QQuickItem derived item.

cursorPosition : int

The position of the cursor in the TextEdit.

cursorRectangle : rectangle

The rectangle where the standard text cursor is rendered within the text edit. Read-only.

The position and height of a custom cursorDelegate are updated to follow the cursorRectangle automatically when it changes. The width of the delegate is unaffected by changes in the cursor rectangle.

cursorVisible : bool

If true the text edit shows a cursor.

This property is set and unset when the text edit gets active focus, but it can also be set directly (useful, for example, if a KeyProxy might forward keys to it).

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.

 
Sélectionnez
TextEdit { 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 TextEdit.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.

 
Sélectionnez
TextEdit { text: "Hello"; renderType: TextEdit.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.

 
Sélectionnez
TextEdit { text: "OATS FLAVOUR WAY"; kerning: font.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 TextEdit::font.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.

 
Sélectionnez
TextEdit { 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

 
Sélectionnez
TextEdit { 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.2] hoveredLink : string

This property was introduced in Qt 5.2.

See Also

See also linkHovered, linkAt()

inputMethodComposing : bool

This property holds whether the TextEdit has partial text input from an input method.

While it is composing an input method may rely on mouse or key events from the TextEdit to edit or commit the partial text. This property can be used to determine when to disable events handlers that may interfere with the correct operation of an input method.

inputMethodHints : enumeration

Provides hints to the input method about the expected content of the text edit and how it should operate.

The value is a bit-wise combination of flags or Qt.ImhNone if no hints are set.

Flags that alter behaviour are:

  • Qt.ImhHiddenText - Characters should be hidden, as is typically used when entering passwords.

  • Qt.ImhSensitiveData - Typed text should not be stored by the active input method in any persistent storage like predictive user dictionary.

  • Qt.ImhNoAutoUppercase - The input method should not try to automatically switch to upper case when a sentence ends.

  • Qt.ImhPreferNumbers - Numbers are preferred (but not required).

  • Qt.ImhPreferUppercase - Upper case letters are preferred (but not required).

  • Qt.ImhPreferLowercase - Lower case letters are preferred (but not required).

  • Qt.ImhNoPredictiveText - Do not use predictive text (i.e. dictionary lookup) while typing.

  • Qt.ImhDate - The text editor functions as a date field.

  • Qt.ImhTime - The text editor functions as a time field.

Flags that restrict input (exclusive flags) are:

  • Qt.ImhDigitsOnly - Only digits are allowed.

  • Qt.ImhFormattedNumbersOnly - Only number input is allowed. This includes decimal point and minus sign.

  • Qt.ImhUppercaseOnly - Only upper case letter input is allowed.

  • Qt.ImhLowercaseOnly - Only lower case letter input is allowed.

  • Qt.ImhDialableCharactersOnly - Only characters suitable for phone dialing are allowed.

  • Qt.ImhEmailCharactersOnly - Only characters suitable for email addresses are allowed.

  • Qt.ImhUrlCharactersOnly - Only characters suitable for UR