【问题标题】:QML adding two inputs and returning resultQML 添加两个输入并返回结果
【发布时间】:2019-02-22 04:00:36
【问题描述】:

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

我做了我的代码,但它没有工作,

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

如你所见,我尝试了上面的代码。

【问题讨论】:

    标签: qt qml v-play felgo


    【解决方案1】:

    TextEdit 中的输入被视为字符串。您必须将其转换为数字:

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

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 2018-08-19
      • 2019-07-23
      • 2019-10-20
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      • 2021-07-01
      • 2018-03-09
      相关资源
      最近更新 更多