简体   繁体   中英

QML adding two inputs and returning result

I have a QML file, by V-Play (aka 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))
            }

        }
    }
}

Which as expected returns my desired window, but now i want it to:

  • Add the two inputs up after click of a button

  • And display it on the TextEdit

I did my code, but it didn't work,

I know you can create functions, but i don't know how to do what i want.

I tried the code above as you see.

The input in your TextEdit is considered as String. You have to convert it into numbers:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM