如何儲存使用者在變數中輸入的值?

如何儲存使用者在變數中輸入的值?

如何儲存使用者在變數中輸入的值?

例如我有這個程式碼:

TextField {width: 37; height: 19
                       var 
                       id: q1
                       x: 91
                       y: 29
                       anchors.horizontalCenter: parent.horizontalCenter
                                anchors.verticalCenter: parent.verticalCenter
                                anchors.centerIn: parent
                       placeholderText: "0"
                       font.pixelSize: 12
                       text: ""
                       anchors.verticalCenterOffset: 26
                       anchors.horizontalCenterOffset: -115
                       validator: IntValidator{}
                       horizontalAlignment: TextInput.AlignHCenter
                       style: TextFieldStyle {
                           textColor: "black"
                           background: Rectangle { width: 45; height: 25; radius: 20.0

                               color: "#F0EBEB"
                               implicitWidth: 40
                               implicitHeight: 24
                               border.color: "#000000"
                               border.width: 1
                           }
                       }
                       onTextChanged: {console.log(parseInt(text,10) + 1000)}
                   }

我可以使用該 id 進行求和或乘法嗎?如果是,那麼如何?

答案1

您可以在 TextField 元件外部重複使用文字屬性。

請看以下範例,其中 Text 元素顯示輸入文字 + 5 的結果:

在此輸入影像描述

對應的代碼:

import QtQuick 2.0
import Ubuntu.Components 0.1
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.1

MainView {
    id: main
    width: 300
    height: 150

    Row {
        width: 37;
        height: 19
        spacing: units.gu(3)

        TextField {
            id: q1
            placeholderText: "0"
            font.pixelSize: 12
            text: "0"
            validator: IntValidator{}
            horizontalAlignment: TextInput.AlignHCenter

            style: TextFieldStyle {
                textColor: "black"
                background: Rectangle {
                    width: 45;
                    height: 25;
                    radius: 20.0
                    color: "#F0EBEB"
                    implicitWidth: 40
                    implicitHeight: 24
                    border.color: "#000000"
                    border.width: 1
                }
            }
        }

        Text {
            height: 25;
            text: "TextField + 5 = "+(parseInt(q1.text, 10) + 5)
        }
    }
}

更新:

若要全域儲存兩個文字輸入的總和,您可以在父元素中定義一個屬性,請查看以下程式碼:

import QtQuick 2.0
import Ubuntu.Components 0.1
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.1

MainView {
    id: main
    width: 300
    height: 300
    property var sum_q1_q2: parseInt(q1.text, 10) + parseInt(q2.text, 10)

    Column {
        width: 37;
        height: 19
        spacing: units.gu(3)

        TextField {
            id: q1
            placeholderText: "0"
            font.pixelSize: 12
            text: "0"
            validator: IntValidator{}
            horizontalAlignment: TextInput.AlignHCenter

            style: TextFieldStyle {
                textColor: "black"
                background: Rectangle {
                    width: 45;
                    height: 25;
                    radius: 20.0
                    color: "#F0EBEB"
                    implicitWidth: 40
                    implicitHeight: 24
                    border.color: "#000000"
                    border.width: 1
                }
            }
        }

        TextField {
            id: q2
            placeholderText: "0"
            font.pixelSize: 12
            text: "0"
            validator: IntValidator{}
            horizontalAlignment: TextInput.AlignHCenter

            style: TextFieldStyle {
                textColor: "black"
                background: Rectangle {
                    width: 45;
                    height: 25;
                    radius: 20.0
                    color: "#F0EBEB"
                    implicitWidth: 40
                    implicitHeight: 24
                    border.color: "#000000"
                    border.width: 1
                }
            }
        }

        Text {
            height: 25;
            text: "Q1 + Q2 = "+main.sum_q1_q2
        }
    }
}

新的 Text 元素現在使用「main」元件的屬性:

在此輸入影像描述

相關內容