8

How do I assign an TextInput to an int, in qml?

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. <<< ?  >>>
    }
mohsen
  • 1,763
  • 3
  • 17
  • 55
user825174
  • 81
  • 1
  • 1
  • 2

2 Answers2

9

It's just a piece of Javascript

Keys.onReturnPressed: new_span_seconds = parseInt(editor.text)
blakharaz
  • 2,580
  • 13
  • 18
0

Take advantage of a jenky part of js, which allows you to convert a string into an integer by "multiplying" it. F.ex. let foo = "2"; let bar = foo * 1.

Here's how it looks when applied to the specific code you gave:

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
    }
}
Kenn Sebesta
  • 7,485
  • 1
  • 19
  • 21