官方描述:在一个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. 内容提供者(Content Provider)——跨程序共享数据

    内容提供者 Content Provider 应用的数据库是不允许其他应用访问的 内容提供者的作用就是让别的应用访问到你的数据库 自定义内容提供者,继承ContentProvider类,重写增删改查方 ...

  2. Codeigniter数据库操作

    //查询: $query = $this->db_query("SELECT * FROM table"); ================================ ...

  3. mybatis update语句参数正常, 数据没有更新

    昨天做的一个功能, 更新每天支付宝账号的所有订单收益 ,有一个update 语句:

  4. centos7 搭建 docker 环境

    1. 安装centos7  http://isoredirect.centos.org/centos/7/isos/x86_64/ 下载 everything 版本,最小化版本可能缺失很多东西 我是在 ...

  5. Spring 4.x org.springframework.http.converter.json.MappingJacksonHttpMessageConverter ClassNotFoundException:

    Spring 4.x The first major version of Jackson is no longer supported in Spring 4. The class you want ...

  6. UVM:8.2.4 factory 机制的调试

    1.UVM提供了print_override_info 帮助debug.以上节new_monitor 为例: 2.调用print_override_info : 结果: 实际调用debug_creat ...

  7. iOS 利用Socket UDP协议广播机制的实现

    1.前言 什么是UDP协议广播机制? 举一个例. 比如在一群人群中,一个人要找张三,于是你向人群里大喊一声(广播):"谁是张三" 假设它是张三,它就会回应你.在网络中也是一样的. ...

  8. WPF中将DataGrid导出Excel

    int number = 1; private void MenuItem_Click(object sender, RoutedEventArgs e) { #region string path ...

  9. 聊聊 KVC 和 KVO 的高阶应用

    KVC, KVO 作为一种魔法贯穿日常Cocoa开发,笔者原先是准备写一篇对其的全面总结,可网络上对其的表面介绍已经够多了,除去基本层面的使用,笔者跟大家谈下平常在网络上没有提及的KVC, KVO进阶 ...

  10. C# StringExt 字符串扩展

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...