TextField.qml 18.59 KiB
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Quick Controls module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
**     of its contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
****************************************************************************/
import QtQuick 2.0
import QtQuick.Controls 1.0
import QtQuick.Controls.Private 1.0
import "Styles"
import "Styles/Settings.js" as Settings
/*!
    \qmltype TextField
    \inqmlmodule QtQuick.Controls 1.0
    \ingroup controls
    \brief TextField displays a single line of editable plain text
    TextField is used to accept a line of text input. Input constraints
    can be placed on a TextField item (for example, through a \l validator or \l inputMask),
    and setting \l echoMode to an appropriate value enables TextField to be used for
    a password input field.
    \sa TextArea, TextInput
Control {
    id: textfield
    /*!
        \qmlproperty bool TextField::acceptableInput
        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).
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
\sa validator, inputMask */ readonly property alias acceptableInput: textInput.acceptableInput // read only /*! \qmlproperty bool TextField::activeFocusOnPress Whether the TextField should gain active focus on a mouse press. By default this is set to \c true. */ property alias activeFocusOnPress: textInput.activeFocusOnPress /*! \qmlproperty bool TextField::canPaste Returns true if the TextField is writable and the content of the clipboard is suitable for pasting into the TextField. */ readonly property alias canPaste: textInput.canPaste /*! \qmlproperty bool TextField::canRedo Returns true if the TextField is writable and there are \l {undo}{undone} operations that can be redone. */ readonly property alias canRedo: textInput.canRedo /*! \qmlproperty bool TextField::canUndo Returns true if the TextField is writable and there are previous operations that can be undone. */ readonly property alias canUndo: textInput.canUndo /*! \qmlproperty color TextField::textColor The text color. */ property alias textColor: textInput.color /*! \qmlproperty int TextField::cursorPosition The position of the cursor in the TextField. */ property alias cursorPosition: textInput.cursorPosition /*! \qmlproperty string TextField::displayText This is the text displayed in the TextField. If \l echoMode is set to TextInput::Normal, this holds the same value as the TextField::text property. Otherwise, this property holds the text visible to the user, while the \l text property holds the actual entered text. */ readonly property alias displayText: textInput.displayText /*! \qmlproperty enumeration TextField::echoMode Specifies how the text should be displayed in the TextField. \list \li TextInput.Normal - Displays the text as it is. (Default) \li TextInput.Password - Displays asterisks instead of characters.
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
\li TextInput.NoEcho - Displays nothing. \li TextInput.PasswordEchoOnEdit - Displays characters as they are entered while editing, otherwise displays asterisks. \endlist */ property alias echoMode: textInput.echoMode /*! \qmlproperty font TextField::font The font of the TextField. */ property alias font: textInput.font /*! \qmlproperty enumeration TextField::horizontalAlignment Sets the alignment of the text within the TextField item's width. By default, the horizontal 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. The valid values for \c horizontalAlignment are \c TextInput.AlignLeft, \c TextInput.AlignRight and \c 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 \c horizontalAlignment will remain unchanged. To query the effective horizontal alignment of TextField, use the read-only property \c effectiveHorizontalAlignment. */ property alias horizontalAlignment: textInput.horizontalAlignment /*! \qmlproperty enumeration TextField::effectiveHorizontalAlignment Gets the effective horizontal alignment of the text within the TextField item's width. To set/get the default horizontal alignment of TextField, use the property \c horizontalAlignment. */ readonly property alias effectiveHorizontalAlignment: textInput.effectiveHorizontalAlignment /*! \qmlproperty enumeration TextField::verticalAlignment Sets the alignment of the text within the TextField item's height. The valid valid values for \c verticalAlignment are \c TextInput.AlignTop, \c TextInput.AlignBottom \c TextInput.AlignVCenter (default). */ property alias verticalAlignment: textInput.verticalAlignment /*! \qmlproperty string TextField::inputMask Allows you to set an input mask on the TextField, restricting the allowable text inputs. See QLineEdit::inputMask for further details, as the exact same mask strings are used by TextField. \sa acceptableInput, validator */ property alias inputMask: textInput.inputMask /*! \qmlproperty enumeration TextField::inputMethodHints Provides hints to the input method about the expected content of the text field and how it should operate. The value is a bit-wise combination of flags, or Qt.ImhNone if no hints are set.
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
The default value is Qt.ImhNone. Flags that alter behavior are: \list \li Qt.ImhHiddenText - Characters should be hidden, as is typically used when entering passwords. This is automatically set when setting echoMode to \c TextInput.Password. \li Qt.ImhSensitiveData - Typed text should not be stored by the active input method in any persistent storage like predictive user dictionary. \li Qt.ImhNoAutoUppercase - The input method should not try to automatically switch to upper case when a sentence ends. \li Qt.ImhPreferNumbers - Numbers are preferred (but not required). \li Qt.ImhPreferUppercase - Upper case letters are preferred (but not required). \li Qt.ImhPreferLowercase - Lower case letters are preferred (but not required). \li Qt.ImhNoPredictiveText - Do not use predictive text (i.e. dictionary lookup) while typing. \li Qt.ImhDate - The text editor functions as a date field. \li Qt.ImhTime - The text editor functions as a time field. \endlist Flags that restrict input (exclusive flags) are: \list \li Qt.ImhDigitsOnly - Only digits are allowed. \li Qt.ImhFormattedNumbersOnly - Only number input is allowed. This includes decimal point and minus sign. \li Qt.ImhUppercaseOnly - Only upper case letter input is allowed. \li Qt.ImhLowercaseOnly - Only lower case letter input is allowed. \li Qt.ImhDialableCharactersOnly - Only characters suitable for phone dialing are allowed. \li Qt.ImhEmailCharactersOnly - Only characters suitable for email addresses are allowed. \li Qt.ImhUrlCharactersOnly - Only characters suitable for URLs are allowed. \endlist Masks: \list \li Qt.ImhExclusiveInputMask - This mask yields nonzero if any of the exclusive flags are used. \endlist */ property alias inputMethodHints: textInput.inputMethodHints /*! \qmlproperty int TextField::length Returns the total number of characters in the TextField item. If the TextField has an inputMask the length will include mask characters and may differ from the length of the string returned by the \l text property. This property can be faster than querying the length the \l text property as it doesn't require any copying or conversion of the TextField's internal string data. */ readonly property alias length: textInput.length /*! \qmlproperty int TextField::maximumLength The maximum permitted length of the text in the TextField. If the text is too long, it is truncated at the limit. */ property alias maximumLength: textInput.maximumLength /*! \qmlproperty string TextField::placeholderText The text that is shown in the text field when the text field is empty and has no focus. */ property alias placeholderText: placeholderTextComponent.text
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
/*! \qmlproperty bool TextField::readOnly Sets whether user input can modify the contents of the TextField. The difference from a disabled text field is that it will appear to be active and text can be selected and copied. 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. */ property alias readOnly: textInput.readOnly /*! \qmlproperty string TextField::selectedText 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. \js myTextField.text.toString().substring(myTextField.selectionStart, myTextField.selectionEnd); \endjs */ readonly property alias selectedText: textInput.selectedText /*! \qmlproperty int TextField::selectionEnd The cursor position after the last character in the current selection. This property is read-only. To change the selection, use select(start,end), selectAll(), or selectWord(). \sa selectionStart, cursorPosition, selectedText */ readonly property alias selectionEnd: textInput.selectionEnd /*! \qmlproperty int TextField::selectionStart The cursor position before the first character in the current selection. This property is read-only. To change the selection, use select(start,end), selectAll(), or selectWord(). \sa selectionEnd, cursorPosition, selectedText */ readonly property alias selectionStart: textInput.selectionStart /*! \qmlproperty string TextField::text The text in the TextField. */ property alias text: textInput.text /*! \qmlproperty Validator TextField::validator Allows you to set a validator on the TextField. When a validator is set the TextField will only accept input which leaves the text property in an 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:
351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
\code import QtQuick 2.0 import QtQuick.Controls 1.0 TextField { validator: IntValidator {bottom: 11; top: 31;} focus: true } \endcode \sa acceptableInput, inputMask */ property alias validator: textInput.validator /*! \qmlsignal TextField::accepted() This signal is emitted when the Return or Enter key is pressed. Note that if there is a \l validator or \l inputMask set on the text field, the signal will only be emitted if the input is in an acceptable state. */ signal accepted() /*! \qmlmethod TextField::copy() Copies the currently selected text to the system clipboard. */ function copy() { textInput.copy() } /*! \qmlmethod TextField::cut() Moves the currently selected text to the system clipboard. */ function cut() { textInput.cut() } /*! \qmlmethod TextField::deselect() Removes active text selection. */ function deselect() { textInput.deselect(); } /*! \qmlmethod string TextField::getText(int start, int end) Removes the section of text that is between the \a start and \a end positions from the TextField. */ function getText(start, end) { return textInput.getText(start, end); } /*! \qmlmethod TextField::insert(int position, string text) Inserts \a text into the TextField at position. */ function insert(position, text) { textInput.insert(position, text); }
421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
/*! \qmlmethod bool TextField::isRightToLeft(int start, int end) Returns true if the natural reading direction of the editor text found between positions \a start and \a end is right to left. */ function isRightToLeft(start, end) { return textInput.isRightToLeft(start, end); } /*! \qmlmethod TextField::paste() Replaces the currently selected text by the contents of the system clipboard. */ function paste() { textInput.paste() } /*! \qmlmethod TextField::redo() Redoes the last operation if redo is \l {canRedo}{available}. */ function redo() { textInput.redo(); } /*! \qmlmethod TextField::select(int start, int end) Causes the text from \a start to \a 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). \sa selectionStart, selectionEnd */ function select(start, end) { textInput.select(start, end) } /*! \qmlmethod TextField::selectAll() Causes all text to be selected. */ function selectAll() { textInput.selectAll() } /*! \qmlmethod TextField::selectWord() Causes the word closest to the current cursor position to be selected. */ function selectWord() { textInput.selectWord() } /*! \qmlmethod TextField::undo() Undoes the last operation if undo is \l {canUndo}{available}. Deselects any current selection, and updates the selection start to the current cursor position. */
491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
function undo() { textInput.undo(); } /*! \internal */ property alias __containsMouse: mouseArea.containsMouse /*! \internal */ property alias __contentHeight: textInput.contentHeight /*! \internal */ property alias __contentWidth: textInput.contentWidth /*! \internal */ style: Qt.createComponent(Settings.THEME_PATH + "/TextFieldStyle.qml", textInput) /*! \internal */ onFocusChanged: { if (textfield.activeFocus) textInput.forceActiveFocus(); } Accessible.name: text Accessible.role: Accessible.EditableText Accessible.description: placeholderText MouseArea { id: mouseArea anchors.fill: parent hoverEnabled: true onClicked: textfield.forceActiveFocus() } TextInput { id: textInput selectByMouse: true selectionColor: __panel ? __panel.selectionColor : "darkred" selectedTextColor: __panel ? __panel.selectedTextColor : "white" font: __panel ? __panel.font : font anchors.leftMargin: __panel ? __panel.leftMargin : 0 anchors.topMargin: __panel ? __panel.topMargin : 0 anchors.rightMargin: __panel ? __panel.rightMargin : 0 anchors.bottomMargin: __panel ? __panel.bottomMargin : 0 anchors.fill: parent verticalAlignment: Text.AlignVCenter color: __panel ? __panel.foregroundColor : "darkgray" clip: true renderType: Text.NativeRendering onAccepted: textfield.accepted() } Text { id: placeholderTextComponent anchors.fill: textInput font: textInput.font horizontalAlignment: textInput.horizontalAlignment verticalAlignment: textInput.verticalAlignment opacity: !textInput.text.length && !textInput.activeFocus ? 1 : 0 color: __panel ? __panel.placeholderTextColor : "darkgray" clip: true elide: Text.ElideRight renderType: Text.NativeRendering Behavior on opacity { NumberAnimation { duration: 90 } } } MouseArea { anchors.fill: parent
561562563564565
cursorShape: Qt.IBeamCursor acceptedButtons: Qt.NoButton } }