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()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值