2011-07-13 17 views
2

我在QML中有一个代码片段,它应该在screen.text中查找正则表达式“调用”,如果找不到,只有这样它才会更改screen.text。很抱歉,QML/QString documentation中的文档不明确。在QML中包含(regexp)什么可能是Qstring /字符串

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 
     } 
    } 

我得到的错误是:

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

回答

3

您必须在处理程序中使用Javascript函数:

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

因为功能 “startsWith” 不是标准功能。

不能说,如果你可以使用QML JS的原型,但您使用此代码:

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

或仅

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

更读到这里:http://www.tek-tips.com/faqs.cfm?fid=6620

0

与其他两个答案一样表示:toString()给出一个JavaScript字符串,而不是QString,而JavaScript字符串没有startsWith()。使用显示的解决方法之一。

相关问题