user825174
user825174

Reputation: 79

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

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. <<< ?  >>>
    }

Upvotes: 7

Views: 11295

Answers (2)

Kenn Sebesta
Kenn Sebesta

Reputation: 9758

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
    }
}

Upvotes: 0

blakharaz
blakharaz

Reputation: 2590

It's just a piece of Javascript

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

Upvotes: 10

Related Questions