官方描述:在一个Qt Quick应用程序中可以使用线程了.

Import Statement:     import QtQuick .
属性:
source : url
信号:
message(jsobject msg)

使用WorkerScript在一个新线程中执行操作.再后台执行操作是很有用的,主GUI线程也不会锁定.

Message可以在新线程和父线程之间通过sendMessage()和onMessage()进行传递消息.
方法:
sendMessage(jsobject message)
案例:

功能是:鼠标点击窗口中的某一个位置,程序中的那行文本便会更新当前鼠标的XY坐标.

运行效果如下图所示:

import QtQuick 2.0

Rectangle {
width: 300; height: 300 Text {
id: myText
text: 'Click anywhere'
} WorkerScript {
id: myWorker
source: "script.js" onMessage: myText.text = messageObject.reply
} MouseArea {
anchors.fill: parent
onClicked: myWorker.sendMessage({ 'x': mouse.x, 'y': mouse.y })
}
}

The above worker script specifies a JavaScript file, "script.js", that handles the operations to be performed in the new thread. Here is script.js:

上面的worker script指定了一个javascript文件,"script.js",在这个新线程中处理将被执行的操作.下面就是这个script.js文件

WorkerScript.onMessage = function(message) {
// ... long-running operations and calculations are done here
WorkerScript.sendMessage({ 'reply': 'Mouse is at ' + message.x + ',' + message.y })
}
当用户在矩形中点击任意位置时,sendMessage()被唤醒,触发了workerScript.onMessage()再script.js中进行处理.这个返回一个回复消息,并且在之后被myWorker的onMessage()句柄接收到.
 
限制:
因为WorkerScript.onMessage()函数是在分开的线程中执行的,所以JavaScript文件从主QML工程中分开的上下文中被评估.这意味着不像常规的导入到QML中的javascript文件那样,上例中的script.js不能存取属性,方法或者其他的qml对象的属性,它也不能通过QQmlContext设置QML对象的任何上下文属性.
另外,在值类型上也有限制.可以发送到或者接受来自workerscript的值.具体见sendMessage()细节.
 

Worker script不能使用.导入语法:
见 Qt Quick Examples - Threading and Threaded ListModel Example.

属性文档:
source : url
这里保存着javascript文件的地址url,里面实现了WorkerScript.onMessage()处理线程操作.

信号文档:
message(jsobject msg)
This signal is emitted when a message msg is received from a worker script in another thread through a call to sendMessage().
通信句柄是onMessage.

方法文档:
sendMessage(jsobject message)
在其他线程中发送给定的消息到script 句柄.其他的worker script句柄可以接收消息,通过onMessage().
消息对象只可以包含一下的值类型:
boolean, number, string
JavaScript objects and arrays
ListModel objects (任何其他类型的 QObject* 是不被允许的.)

所有的对象和数组都被拷贝到message上.除了ListModel对象,在消息中任何被其他线程修改了的发送出来的信息 将不会再原始对象上反映出来.

WorkerScript QML Type的更多相关文章

  1. PinchArea QML Type

    PinchArea类型是在QtQuick 1.1中添加进去的.PinchArea是一个不可见的对象,常用在与一个可见对象连接在一起,为对应的可见对象提供手势操作.enabled属性被用来去设置绑定对象 ...

  2. PinchEvent QML Type

    PinchEvent类型在QtQuick 1.1中被添加进来.center, startCenter, previousCenter属性保存了两个触摸点之间的中心位置.scale and previo ...

  3. 初学QML之qmlRegisterType

    qmlRegisterType 是一个可以将C++实现的类在QML中调用的,连接C++和QML的一个工具 首先来看QtAssistant的介绍 int qmlRegisterType(const ch ...

  4. QML Object Attributes QML对象属性

    QML Object Attributes Every QML object type has a defined set of attributes. Each instance of an obj ...

  5. qml demo分析(threading-线程任务)

    一.关键类说明 qml内置了WorkerScript组件,该组件有一个source属性,可以加载js文件,含有一个名为message的信号,意味着他有一个默认的onMessage槽函数,除此之外他还有 ...

  6. Qt QML referenceexamples attached Demo hacking

    /********************************************************************************************* * Qt ...

  7. Qt qml的软件架构设计

    google: qt qml application architecture 有很多资源. 1 https://www.ics.com/blog/multilayered-architecture- ...

  8. Import Statements 导入语句

    Syntax of an Import Statement 导入语句的语法 An import statement allows clients to tell the engine which mo ...

  9. 创建一个QT for Android的传感器应用应用程序(摘自笔者2015年将出的《QT5权威指南》,本文为试读篇)

     这个手册描述了使用Qt Quick面访的方式在Android和ios设备上开发QtQuick应用程序的方法.我们使用Qt Creator实现一个QtQuick应用程序,这个应用程序基于加速器的值 ...

随机推荐

  1. 原生Javascript插件开发实践

    前言 之前公司设计的网站比较混乱,很多地方不统一,其中一个就是弹出层,导致这个原因是因为,公司的UI换了好几个人,而他们每个人做出来的都不太一样.最近公司开始整顿这个问题,对于统一的这种东西当然是做成 ...

  2. 【转】PyDev for Eclipse 简介

      转自:http://www.ibm.com/developerworks/cn/opensource/os-cn-ecl-pydev/index.html PyDev for Eclipse 是一 ...

  3. shell查找文件并删除

    -mtime 0 表示文件改动时间距离当前为0天的文件.即距离当前时间不到1天(24小时)以内的文件. -mtime 1 表示文件改动时间距离当前为1天的文件,即距离当前时间1天(24小时-48小时) ...

  4. 解析xlsx与xls--使用2012poi.jar

    1.导入Jar包:poi-3.8-20120326.jar.poi-ooxml-3.8-20120326.jar.poi-ooxml-schemas-3.8-20120326.jar.xbean.ja ...

  5. [D3] 12. Basic Transitions with D3

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. PrintWriter的print和write方法(转)

    public void print(String s) {if (s == null) {s = "null";}write(s);  }  print只是先对s==null转换为 ...

  7. DTrace Probes in HotSpot VM----java

    http://docs.oracle.com/javase/6/docs/technotes/guides/vm/dtrace.html http://docs.oracle.com/javase/7 ...

  8. mysql 导入excel 或 .csv

    第一步 导出excel 去掉列头,设置文本里面格式

  9. javascript中的函数返回值(return)

    有些情况,我们希望获取到函数的执行结果,也就是我们需要在函数以外的地方处理执行结果,而不是在函数内部处理.这时我们就需要为函数设一个返回值,也就是return,即函数执行完毕以后返回的结果. 若在函数 ...

  10. Chrome浏览器离线下载地址(Stable/Beta/Dev)

    最新稳定版:https://www.google.com/intl/zh-CN/chrome/browser/?standalone=1 最新测试版:https://www.google.com/in ...