Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 466
Arnab Datta Avatar asked Jul 13 '11 11:07

Arnab Datta


2 Answers

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
    }
like image 119
hmuelner Avatar answered Sep 26 '22 23:09

hmuelner


Because function "startsWith" is not standard function.

Can't say if you can use prototypes in QML JS but you use this code:

String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str)}

or only

if(screen.text.toString().match("^Calling")==screen.text.toString())

more to read here: http://www.tek-tips.com/faqs.cfm?fid=6620

like image 34
Marek Sebera Avatar answered Sep 22 '22 23:09

Marek Sebera