Qt 4.7: QML TextInput Element

Qt Reference Documentation

# QML TextInput Element

The TextInput item displays an editable line of text. More...

Inherits Item

This element was introduced in Qt 4.7.

- List of all members, including inherited members

## Properties

- acceptableInput : bool
- activeFocusOnPress : bool
- autoScroll : bool
- canPaste : bool
- color : color
- cursorDelegate : Component
- cursorPosition : int
- cursorVisible : bool
- displayText : string
- echoMode : enumeration
- font.bold : bool
- font.capitalization : enumeration
- font.family : string
- font.italic : bool
- font.letterSpacing : real
- font.pixelSize : int
- font.pointSize : real
- font.strikeout : bool
- font.underline : bool
- font.weight : enumeration
- font.wordSpacing : real
- horizontalAlignment : enumeration
- inputMask : string
- inputMethodComposing : bool
- maximumLength : int
- mouseSelectionMode : enum
- passwordCharacter : string
- readOnly : bool
- selectByMouse : bool
- selectedText : string
- selectedTextColor : color
- selectionColor : color
- selectionEnd : int
- selectionStart : int
- smooth : bool
- text : string
- validator : Validator

## Signals

- onAccepted

## Methods

- closeSoftwareInputPanel
- copy
- cut
- deselect
- isRightToLeft
- moveCursorSelection
- openSoftwareInputPanel
- paste
- positionAt
- positionToRectangle
- select
- selectAll
- selectWord

## Detailed Description

The TextInput element displays a single line of editable plain text.

TextInput is used to accept a line of text input. Input constraints can be placed on a TextInput item (for example, through a validator or inputMask), and setting echoMode to an appropriate value enables TextInput to be used for a password input field.

On Mac OS X, the Up/Down key bindings for Home/End are explicitly disabled. If you want such bindings (on any platform), you will need to construct them in QML.

See also TextEdit, Text, and Text Selection example.

## Property Documentation

read-onlyacceptableInput : bool

This property is always true unless a validator or input mask has been set. If a validator or input mask has been set, this property will only be true if the current text is acceptable to the validator or input mask as a final string (not as an intermediate string).

activeFocusOnPress : bool

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

autoScroll : bool

Whether the TextInput should scroll when the text is longer than the width. By default this is set to true.

read-onlycanPaste : bool

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

This property group was introduced in QtQuick 1.1.

color : color

The text color.

cursorDelegate : Component

The delegate for the cursor in the TextInput.

If you set a cursorDelegate for a TextInput, 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 TextInput when a cursor is needed, and the x property 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 QDeclarativeItem or QDeclarativeItem derived item.

cursorPosition : int

The position of the cursor in the TextInput.

cursorVisible : bool

Set to true when the TextInput shows a cursor.

This property is set and unset when the TextInput gets active focus, so that other properties can be bound to whether the cursor is currently showing. As it gets set and unset automatically, when you set the value yourself you must keep in mind that your value may be overwritten.

It can be set directly in script, for example if a KeyProxy might forward keys to it and you desire it to look active when this happens (but without actually giving it active focus).

It should not be set directly on the element, like in the below QML, as the specified value will be overridden an lost on focus changes.

     TextInput {
         text: "Text"
         cursorVisible: false
     }

In the above snippet the cursor will still become visible when the TextInput gains active focus.

read-onlydisplayText : string

This is the text displayed in the TextInput.

If echoMode is set to TextInput::Normal, this holds the same value as the TextInput::text property. Otherwise, this property holds the text visible to the user, while the text property holds the actual entered text.

echoMode : enumeration

Specifies how the text should be displayed in the TextInput.

- TextInput.Normal - Displays the text as it is. (Default)
- TextInput.Password - Displays asterixes instead of characters.
- TextInput.NoEcho - Displays nothing.
- TextInput.PasswordEchoOnEdit - Displays all but the current character as asterixes.

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.

     TextInput { 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.

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.

font.weight : enumeration

Sets the font's weight.

The weight can be one of:

- Font.Light
- Font.Normal - the default
- Font.DemiBold
- Font.Bold
- Font.Black

     TextInput { 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.

horizontalAlignment : enumeration

Sets the horizontal alignment of the text within the TextInput 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.

TextInput does not have vertical alignment, as the natural height is exactly the height of the single line of text. If you set the height manually to something larger, TextInput will always be top aligned vertically. You can use anchors to align it however you want within another item.

The valid values for `horizontalAlignment` are `TextInput.AlignLeft`, `TextInput.AlignRight` and `TextInput.AlignHCenter`.

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 TextInput, use the property LayoutMirroring::enabled.

inputMask : string

Allows you to set an input mask on the TextInput, restricting the allowable text inputs. See QLineEdit::inputMask for further details, as the exact same mask strings are used by TextInput.

See also acceptableInput and validator.

read-onlyinputMethodComposing : bool

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

While it is composing an input method may rely on mouse or key events from the TextInput 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.

This property group was introduced in QtQuick 1.1.

maximumLength : int

The maximum permitted length of the text in the TextInput.

If the text is too long, it is truncated at the limit.

By default, this property contains a value of 32767.

mouseSelectionMode : enum

Specifies how text should be selected using a mouse.

- TextInput.SelectCharacters - The selection is updated with individual characters. (Default)
- TextInput.SelectWords - The selection is updated with whole words.

This property only applies when selectByMouse is true.

This property group was introduced in QtQuick 1.1.

passwordCharacter : string

This is the character displayed when echoMode is set to Password or PasswordEchoOnEdit. By default it is an asterisk.

If this property is set to a string with more than one character, the first character is used. If the string is empty, the value is ignored and the property is not set.

readOnly : bool

Sets whether user input can modify the contents of the TextInput.

If readOnly is set to true, then user input will not affect the text property. Any bindings or attempts to set the text property will still work.

selectByMouse : bool

Defaults to false.

If true, the user can use the mouse to select text in some platform-specific way. Note that for some platforms this may not be an appropriate interaction (eg. may conflict with how the text needs to behave inside a Flickable.

read-onlyselectedText : string

This read-only property provides the text currently selected in the text input.

It is equivalent to the following snippet, but is faster and easier to use.

     myTextInput.text.toString().substring(myTextInput.selectionStart,
         myTextInput.selectionEnd);

selectedTextColor : color

The highlighted text color, used in selections.

selectionColor : color

The text highlight color, used behind selections.

read-onlyselectionEnd : int

The cursor position after the last character in the current selection. Setting this and selectionStart allows you to specify a selection in the text edit.

Note that if selectionStart == selectionEnd then there is no current selection.

See also selectionStart, cursorPosition, selectedText, and select().

read-onlyselectionStart : int

The cursor position before the first character in the current selection. Setting this and selectionEnd allows you to specify a selection in the text edit.

Note that if selectionStart == selectionEnd then there is no current selection.

See also selectionEnd, cursorPosition, selectedText, and select().

smooth : bool

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.

text : string

The text in the TextInput.

validator : Validator

Allows you to set a validator on the TextInput. When a validator is set the TextInput will only accept input which leaves the text property in an acceptable or intermediate state. The accepted signal will only be sent if the text is in an acceptable state when enter is pressed.

Currently supported validators are IntValidator, DoubleValidator and RegExpValidator. An example of using validators is shown below, which allows input of integers between 11 and 31 into the text input:

     import QtQuick 1.0
     TextInput{
         validator: IntValidator{bottom: 11; top: 31;}
         focus: true
     }

See also acceptableInput and inputMask.

## Signal Documentation

TextInput::onAccepted ()

This handler is called when the Return or Enter key is pressed. Note that if there is a validator or inputMask set on the text input, the handler will only be emitted if the input is in an acceptable state.

## Method Documentation

void TextInput::closeSoftwareInputPanel ()

Closes a software input panel like a virtual keyboard shown on the screen, useful for customizing when you want the input keyboard to be shown and hidden in your application.

By default the opening of input panels follows the platform style. On Symbian^1 and Symbian^3 -based devices the panels are opened by clicking TextInput. On other platforms the panels are automatically opened when TextInput element gains active focus. Input panels are always closed if no editor has active focus.

. You can disable the automatic behavior by setting the property `activeFocusOnPress` to false and use functions openSoftwareInputPanel() and closeSoftwareInputPanel() to implement the behavior you want.

Only relevant on platforms, which provide virtual keyboards.

     import QtQuick 1.0
     TextInput {
         id: textInput
         text: "Hello world!"
         activeFocusOnPress: false
         MouseArea {
             anchors.fill: parent
             onClicked: {
                 if (!textInput.activeFocus) {
                     textInput.forceActiveFocus();
                     textInput.openSoftwareInputPanel();
                 } else {
                     textInput.focus = false;
                 }
             }
             onPressAndHold: textInput.closeSoftwareInputPanel();
         }
     }

TextInput::copy ()

Copies the currently selected text to the system clipboard.

TextInput::cut ()

Moves the currently selected text to the system clipboard.

void TextInput::deselect ()

Removes active text selection.

This documentation was introduced in QtQuick 1.1.

void TextInput::isRightToLeft ( int start, int end )

Returns true if the natural reading direction of the editor text found between positions start and end is right to left.

void TextInput::moveCursorSelection ( int position, SelectionMode mode = TextInput.SelectCharacters )

Moves the cursor to position and updates the selection according to the optional mode parameter. (To only move the cursor, set the cursorPosition property.)

When this method is called it additionally sets either the selectionStart or the selectionEnd (whichever was at the previous cursor position) to the specified position. This allows you to easily extend and contract the selected text range.

The selection mode specifies whether the selection is updated on a per character or a per word basis. If not specified the selection mode will default to TextInput.SelectCharacters.

- TextEdit.SelectCharacters - Sets either the selectionStart or selectionEnd (whichever was at the previous cursor position) to the specified position.
- TextEdit.SelectWords - Sets the selectionStart and selectionEnd to include all words between the specified postion and the previous cursor position. Words partially in the range are included.

For example, take this sequence of calls:

     cursorPosition = 5
     moveCursorSelection(9, TextInput.SelectCharacters)
     moveCursorSelection(7, TextInput.SelectCharacters)

This moves the cursor to position 5, extend the selection end from 5 to 9 and then retract the selection end from 9 to 7, leaving the text from position 5 to 7 selected (the 6th and 7th characters).

The same sequence with TextInput.SelectWords will extend the selection start to a word boundary before or on position 5 and extend the selection end to a word boundary on or past position 9.

This documentation was introduced in QtQuick 1.1.

void TextInput::openSoftwareInputPanel ()

Opens software input panels like virtual keyboards for typing, useful for customizing when you want the input keyboard to be shown and hidden in your application.

By default the opening of input panels follows the platform style. On Symbian^1 and Symbian^3 -based devices the panels are opened by clicking TextInput. On other platforms the panels are automatically opened when TextInput element gains active focus. Input panels are always closed if no editor has active focus.

. You can disable the automatic behavior by setting the property `activeFocusOnPress` to false and use functions openSoftwareInputPanel() and closeSoftwareInputPanel() to implement the behavior you want.

Only relevant on platforms, which provide virtual keyboards.

     import QtQuick 1.0
     TextInput {
         id: textInput
         text: "Hello world!"
         activeFocusOnPress: false
         MouseArea {
             anchors.fill: parent
             onClicked: {
                 if (!textInput.activeFocus) {
                     textInput.forceActiveFocus()
                     textInput.openSoftwareInputPanel();
                 } else {
                     textInput.focus = false;
                 }
             }
             onPressAndHold: textInput.closeSoftwareInputPanel();
         }
     }

TextInput::paste ()

Replaces the currently selected text by the contents of the system clipboard.

int TextInput::positionAt ( int x, CursorPosition position = CursorBetweenCharacters )

This function returns the character position at x pixels from the left of the textInput. Position 0 is before the first character, position 1 is after the first character but before the second, and so on until position text.length, which is after all characters.

This means that for all x values before the first character this function returns 0, and for all x values after the last character this function returns text.length.

The cursor position type specifies how the cursor position should be resolved.

- TextInput.CursorBetweenCharacters - Returns the position between characters that is nearest x.
- TextInput.CursorOnCharacter - Returns the position before the character that is nearest x.

This documentation was introduced in QtQuick 1.1.

rect TextInput::positionToRectangle ( int pos )

This function takes a character position and returns the rectangle that the cursor would occupy, if it was placed at that character position.

This is similar to setting the cursorPosition, and then querying the cursor rectangle, but the cursorPosition is not changed.

void TextInput::select ( int start, int end )

Causes the text from start to end to be selected.

If either start or end is out of range, the selection is not changed.

After calling this, selectionStart will become the lesser and selectionEnd will become the greater (regardless of the order passed to this method).

See also selectionStart and selectionEnd.

void TextInput::selectAll ()

Causes all text to be selected.

void TextInput::selectWord ()

Causes the word closest to the current cursor position to be selected.