QML之javascript

1、比用在web的js語法要更嚴格。它不能修改全局對象,不能給未聲明的對象賦值      

      this只能使用在attached信號處理中。 

      Component.onCompleted{

            console.log(this)

      }

2、用在哪些地方?

      用於自定義function

      在信號的處理中使用。例如: MouseArea.onClicked

      放在獨立.js文件,然後被當作resource import進來。這裏有2種情況:

             A、myjs1.js作爲Code-Behind被引入到myqml.qml中

                   若myjs1中沒有其他import,則myjs1文件能看到myqml.qml中所有的變量。就像js文件中所有的函數或者變量定義在qml文件中一樣(這是因爲有可能導入的是QML文件?)

                   若myjs1中還有import myjs2.js,則myjs1和qml文件擁有不同的作用域

            B、myjs1.js作爲shared資源被引入。格式:

                  .pragma library                         

  • a script with imports will not inherit imports from the QML document which imported it (so accessing Component.errorString will fail, for example)
  • a script without imports will inherit imports from the QML document which imported it (so accessing Component.errorString will succeed, for example)
  • a shared script (i.e., defined as .pragma library) does not inherit imports from any QML document even if it imports no other scripts or modules

      用在property binding中

3、支持可直接訪問的List of JavaScript Objects and Functions

      全局對象:

      Math :比較常用

      Date:時間日期

      RegExp:正則表達式

      Array:數組

      Number:數字轉換

      String:字符操作。QML中支持arg成員函數

      全局函數:

      eval(x); parseInt(string,radix); parseFloat(string); 等

       自定義的屬性值:

       NaN、 Infinity、undefined

       需要詳細的。。。

4、QML定義的全局對象和函數

      console:調試用

      Qt:這個比較強大

      qsTr(), qsTranslate(), qsTrId()等其他國際化需要的,支持動態切換語言

      gc(): garbage collection

      print():調試

      XMLHttpRequest,DOMException:

5、使用js動態創建Component

       創建:

      從文件中創建:Qt.createComponent -> createObject()

      使用字符串創建:Qt.createQmlObject

      (使用Loader也可動態創建)

      管理:

      涉及到creation context,沒看明白

      刪除:

      使用Component.destroy

      只能刪除動態建立的Component,例如:

      Item{

          Rectangle{ id: rect

              width:100;height:100;color:"red"

              NumberAnimation on opacity{

                    to:0; duration:100; 

                    onRunningChanged{

                           if(!running) rect.destroy();

              }

          }

      } 

6、importing

     QML文件導入JS,import "resourceurl" as qualifier。

     JS文件導入JS,      .import "resourceurl" as qualifier

     JS文件導入QML,   .import typename majorversion.minorversion as qualifier

     JS文件包含JS文件  Qt.include()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章