2011-07-13 19 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()。使用顯示的解決方法之一。

相關問題