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

10,219

Solution 1

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
    }

Solution 2

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

Solution 3

Like the other two answers indicate: toString() gives a JavaScript string, not a QString, and the JavaScript string does not have a startsWith(). Use one of the workarounds shown.

Share:
10,219

Related videos on Youtube

Arnab Datta
Author by

Arnab Datta

Updated on May 03, 2022

Comments

  • Arnab Datta
    Arnab Datta about 2 years

    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.