如何在qml中为一个int分配一个TextInput?

时间:2011-07-01 16:04:01

标签: qt qml textinput qt-quick

如何在qml中为TextInput分配一个int?

int new_span_seconds

TextInput {
        id: editor
        width: 80
        height: 17
        color: "white"
        font.bold: true; font.pixelSize: 14
        text: "21"
        horizontalAlignment: TextInput.AlignHCenter

    }

    Keys.forwardTo: [ (returnKey), (editor)]

    Item {
        id: returnKey
        Keys.onReturnPressed: new_span_seconds = editor. <<< ?  >>>
        Keys.onEnterPressed:  new_span_seconds = editor. <<< ?  >>>
    }

2 个答案:

答案 0 :(得分:3)

这只是一段Javascript

Keys.onReturnPressed: new_span_seconds = parseInt(editor.text)

答案 1 :(得分:0)

利用 js 的 jenky 部分,它允许您通过“乘法”将字符串转换为整数。例如let foo = "2"; let bar = foo * 1

以下是应用于您提供的特定代码时的外观:

TextInput {
        id: editor
        width: 80
        height: 17
        color: "white"
        font.bold: true; font.pixelSize: 14
        text: "21"
        horizontalAlignment: TextInput.AlignHCenter

    }

    Keys.forwardTo: [ (returnKey), (editor)]

    Item {
        id: returnKey
        Keys.onReturnPressed: new_span_seconds = editor.text * 1
        Keys.onEnterPressed:  new_span_seconds = editor.text * 1
    }
}