簡體   English   中英

QML 添加兩個輸入並返回結果

[英]QML adding two inputs and returning result

我有一個 QML 文件,由 V-Play(又名 Felgo)提供:

import VPlayApps 1.0
import QtQuick 2.0

App {
    id: app
    // You get free licenseKeys from https://v-play.net/licenseKey
    // With a licenseKey you can:
    //  * Publish your games & apps for the app stores
    //  * Remove the V-Play Splash Screen or set a custom one (available with the Pro Licenses)
    //  * Add plugins to monetize, analyze & improve your apps (available with the Pro Licenses)
    //licenseKey: "<generate one from https://v-play.net/licenseKey>"

    NavigationStack {

        Page {
            title: qsTr("My page")

        }

        AppTextField {
            id: appTextField
            x: 0
            y: 329
            width: 256
            height: 19
            anchors.centerIn: parent
            placeholderText: qsTr('Enter a Number')
        }

        AppTextField {
            id: appTextField1
            x: 0
            y: 329
            width: 256
            height: 19
            anchors.verticalCenterOffset: 50
            anchors.centerIn: parent
            placeholderText: qsTr('Enter a Number')
        }
        TextEdit {
            id: text1
            x: 0
            y: 620
            width: 24
            height: 20
            text: qsTr('A')
            font.pixelSize: 30
            anchors.horizontalCenter: appTextField1.horizontalCenter
        }

        AppButton {
            id: button
            x: 0
            y: 575
            width: 24
            height: 20
            text: qsTr("Click me please!")
            anchors.horizontalCenter: appTextField1.horizontalCenter
            enabled: true
            onClicked: {
                text1.text=qsTr('Sum: '+(appTextField.text+appTextField1.text))
            }

        }
    }
}

正如預期的那樣返回我想要的窗口,但現在我希望它:

  • 單擊按鈕后將兩個輸入相加

  • 並將其顯示在TextEdit

我做了我的代碼,但它不起作用,

我知道你可以創建函數,但我不知道如何做我想做的。

如您所見,我嘗試了上面的代碼。

TextEdit的輸入被視為字符串。 您必須將其轉換為數字:

onClicked: {
  const v1 = parseInt(appTextField.text)
  const v2 = parseInt(appTextField1.text)
  text1.text=qsTr('Sum: ' + ( v1 + v2))
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM