将文字附加到TextInput

时间:2015-10-07 10:18:20

标签: qt qml qtquick2

我正在处理QML PIN条目Item,如以下屏幕截图所示:

enter image description here

现在,在Item的底部有一个QML TextInput字段,名为ueKeypadPinFieldField,如UeKeypad的代码所示:

import QtQuick 2.0
import QtQuick.Layouts 1.2
import QtQuick.Controls 1.3

import "../items"

Item
{
    id: ueKeypad

    property string ueParamWorkerImage
    property string ueParamWorkerName

    width: 512
    height: 512
    antialiasing: true

    Rectangle
    {
        id: ueKeypadWrapper
        radius: 16
        border.color: "#4682b4"
        border.width: 1
        antialiasing: true

        anchors.fill: parent

        gradient: Gradient
        {
            GradientStop
            {
                position: 0
                color: "#636363"
            }   // GradientStop

            GradientStop
            {
                position: 1
                color: "#303030"
            }   // GradientStop
        }   // Gradient

        ColumnLayout
        {
            id: ueKeypadLayoutMain
            antialiasing: true

            layoutDirection: Qt.LeftToRight

            spacing: 8

            anchors.fill: parent

            ColumnLayout
            {
                id: ueKeypadTitleLayout

                layoutDirection: Qt.LeftToRight

                Layout.fillWidth: true

                Layout.minimumHeight: 24
                Layout.preferredHeight: 24
                Layout.maximumHeight: 24
                Layout.margins: 8

                Text
                {
                    Layout.fillWidth: true
                    Layout.fillHeight: true

                    text: qsTr("PIN ENTRY")
                    clip: true
                    font.bold: true
                    font.pointSize: 24
                    textFormat: Text.RichText
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignHCenter
                }   // Text
            }   // ColumnLayout

            ColumnLayout
            {
                id: ueUserInfoLayout

                Layout.fillWidth: true
                Layout.fillHeight: true
                Layout.margins: 8

                Image
                {
                    id: ueKeypadWorkerImage
                    clip: true
                    fillMode: Image.PreserveAspectFit

                    source: ueParamWorkerImage

                    Layout.fillWidth: true

                    Layout.minimumHeight: 96
                    Layout.preferredHeight: 96
                    Layout.maximumHeight: 96
                    Layout.alignment: Qt.AlignCenter
                }   // Image

                Text
                {
                    id: ueKeypadWorkerName
                    color: "#ffffff"
                    text: qsTr("TEST")

                    enabled: false
                    antialiasing: true

                    font.bold: false
                    font.pointSize: 24

                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter

                    Layout.fillWidth: true
                    Layout.fillHeight: true
                }   // Text
            }   // ColumnLayout

            GridLayout
            {
                id: ueKeypadNumbersLayout

                Layout.fillWidth: true
                Layout.fillHeight: true
                Layout.margins: 8

                Layout.alignment: Qt.AlignHCenter|Qt.AlignVCenter

                layoutDirection: Qt.LeftToRight

                columnSpacing: 8
                rowSpacing: 8

                flow: GridLayout.LeftToRight

                columns: 3

                UeButton
                {
                    id: ueKeypadButton1

                    Layout.fillWidth: true
                    Layout.fillHeight: true

                    ueText: qsTr("1")
                    //ueKeypadPinFieldField.text: ueKeypadPinFieldField.text+"1"
                }

                UeButton
                {
                    id: ueKeypadButton2

                    Layout.fillWidth: true
                    Layout.fillHeight: true

                    ueText: qsTr("2")
                }

                UeButton
                {
                    id: ueKeypadButton3

                    Layout.fillWidth: true
                    Layout.fillHeight: true

                    ueText: qsTr("3")
                }

                UeButton
                {
                    id: ueKeypadButton4

                    Layout.fillWidth: true
                    Layout.fillHeight: true

                    ueText: qsTr("4")
                }

                UeButton
                {
                    id: ueKeypadButton5

                    Layout.fillWidth: true
                    Layout.fillHeight: true

                    ueText: qsTr("5")
                }

                UeButton
                {
                    id: ueKeypadButton6

                    Layout.fillWidth: true
                    Layout.fillHeight: true

                    ueText: qsTr("6")
                }

                UeButton
                {
                    id: ueKeypadButton7

                    Layout.fillWidth: true
                    Layout.fillHeight: true

                    ueText: qsTr("7")
                }

                UeButton
                {
                    id: ueKeypadButton8

                    Layout.fillWidth: true
                    Layout.fillHeight: true

                    ueText: qsTr("8")
                }

                UeButton
                {
                    id: ueKeypadButton9

                    Layout.fillWidth: true
                    Layout.fillHeight: true

                    ueText: qsTr("9")
                }

                UeButton
                {
                    id: ueKeypadButton0

                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    Layout.columnSpan: 3

                    ueText: qsTr("0")

                }   // UeButton
            }   // GridLayout

            RowLayout
            {
                id: ueKeypadPinFieldLayout
                antialiasing: true

                Layout.fillWidth: true
                Layout.fillHeight: true

                TextInput
                {
                    id: ueKeypadPinFieldField
                    cursorVisible: false
                    enabled: false
                    antialiasing: true
                    readOnly: true
                    activeFocusOnPress: false
                    echoMode: TextInput.PasswordEchoOnEdit
                    font.bold: false
                    font.pointSize: 24
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter

                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    Layout.margins: 8

                    maximumLength: 4

                    inputMask: "9999"

                    selectByMouse: false

                    text: ""
                }
            }

            RowLayout
            {
                id: ueKeypadActionLayout

                Layout.fillWidth: true
                Layout.fillHeight: true
                Layout.margins: 8

                //Layout.alignment: Qt.AlignHCenter|Qt.AlignVCenter

                layoutDirection: Qt.LeftToRight

                spacing: 8

                UeButton
                {
                    id: ueKeypadButtonLogin

                    Layout.fillWidth: true
                    Layout.fillHeight: true

                    ueText: qsTr("Login")
                }   // UeButton

                UeButton
                {
                    id: ueKeypadButtonClear

                    Layout.fillWidth: true
                    Layout.fillHeight: true

                    ueText: qsTr("Clear")

                    onUeSignalButtonClicked:
                    {
                        ueKeypadPinFieldField.text="";
                    }   // onClicked
                }   // UeButton
            }   // RowLayout
        }   // ColumnLayout
    }   // Rectangle
}   // Item

现在,如果我通过数字按钮输入4位数针,如何将文字追加到ueKeypadPinFieldField?我知道onClicked处理程序,但如何在UeButton内附加该事件处理程序中的数字?

1 个答案:

答案 0 :(得分:3)

像这样:

Repeater {
    model: [qsTr("1"), qsTr("2"), qsTr("3"), qsTr("4"), qsTr("5"), qsTr("6"), qsTr("7"), qsTr("8"), qsTr("9"), qsTr("0")]

    Button {
        Layout.fillWidth: true
        Layout.fillHeight: true
        Layout.columnSpan: index == 9 ? 3 : 1

        text: modelData
        onClicked: ueKeypadPinFieldField.text += modelData
    }
}

请注意,我将所有Button移动到转发器中,因为它更整洁。不幸的是qsTr()参数must be literals,否则你可以通过在for循环中动态填充模型来使它更整洁。