Search code examples
javascriptqtqmlqstringqt-quick

contains(regexp) on what is possibly a Qstring/string in QML


I have a code snippet in QML which should look for the regexp "Calling" in screen.text, and if it is not found, only then does it change the screen.text.Unfortunately, the documentation is not clear in QML/QString documentation.

  Button{
        id: call
        anchors.top: seven.bottom
        anchors.left: seven.left

        text: "Call"
        width: 40

        onClicked:{
            if(screen.text.toString().startsWith("Calling" , false))
                return;
            else
                screen.text = "Calling " + screen.text
        }
    }

The error I get is :

file:///home/arnab/workspace/desktop/examples/cellphone.qml:127: TypeError: Result of expression 'screen.text.toString().startsWith' [undefined] is not a function.


Solution

  • You have to use Javascript functions in the handler:

            onClicked:{
            var patt = /^Calling/;
            if(patt.test(screen.text))
                return;
            else
                screen.text = "Calling " + screen.text
        }