代码之家  ›  专栏  ›  技术社区  ›  U13-Forward

QML添加两个输入并返回结果

  •  0
  • U13-Forward  · 技术社区  · 6 年前

    我有一个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

    我做了我的代码,但没用,

    我知道你可以创建函数,但我不知道怎么做我想要的。

    0 回复  |  直到 6 年前
        1
  •  3
  •   Dimitry Ernot    6 年前

    在你的 TextEdit 被视为字符串。你必须把它转换成数字:

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