Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qml, how to parse text to number



  • i have a Text { text: "15"; id: label }. how do i parse the text into number to make some verification (if(label's text is minor than 15) add 1 to label's text and write it into label's text)



  • Maybe you can write a simple javascript function in your current scope.
    @function convertToNumber(string)
    {
    return parseInt(string)
    }@
    And then call the function in your if condition expression?
    Bill



  • you are a qt's dragon! =)
    so, i need to learn javascript. I miss it too!

    just a question: where must i write this function...and how can i call it?



  • You may be able to write it inside your current scope ie.
    The following code is working on my PC, it is incrementing the string value "15" to string value "16":

    @Rectangle {
    width: 360
    height: 360
    Text {
    anchors.centerIn: parent
    text: incrementString("15");
    }

    function incrementString(string)
    {
           var stringValue = String(parseInt(string) + 1);
           return stringValue;
    }
    
    
    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }
    

    }
    @



  • link @ http://doc.qt.nokia.com/4.7/qml-extending-types.html should help you on how to add functions and signal handlers to your qml code.


Log in to reply