6

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.

Lstor
  • 2,265
  • 17
  • 25
Arnab Datta
  • 5,356
  • 10
  • 41
  • 67

3 Answers3

6

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
    }
hmuelner
  • 8,093
  • 1
  • 28
  • 39
1

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

Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
1

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.

Lstor
  • 2,265
  • 17
  • 25