WorkerScript QML Type
官方描述:在一个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 })
}
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的更多相关文章
- PinchArea QML Type
PinchArea类型是在QtQuick 1.1中添加进去的.PinchArea是一个不可见的对象,常用在与一个可见对象连接在一起,为对应的可见对象提供手势操作.enabled属性被用来去设置绑定对象 ...
- PinchEvent QML Type
PinchEvent类型在QtQuick 1.1中被添加进来.center, startCenter, previousCenter属性保存了两个触摸点之间的中心位置.scale and previo ...
- 初学QML之qmlRegisterType
qmlRegisterType 是一个可以将C++实现的类在QML中调用的,连接C++和QML的一个工具 首先来看QtAssistant的介绍 int qmlRegisterType(const ch ...
- QML Object Attributes QML对象属性
QML Object Attributes Every QML object type has a defined set of attributes. Each instance of an obj ...
- qml demo分析(threading-线程任务)
一.关键类说明 qml内置了WorkerScript组件,该组件有一个source属性,可以加载js文件,含有一个名为message的信号,意味着他有一个默认的onMessage槽函数,除此之外他还有 ...
- Qt QML referenceexamples attached Demo hacking
/********************************************************************************************* * Qt ...
- Qt qml的软件架构设计
google: qt qml application architecture 有很多资源. 1 https://www.ics.com/blog/multilayered-architecture- ...
- Import Statements 导入语句
Syntax of an Import Statement 导入语句的语法 An import statement allows clients to tell the engine which mo ...
- 创建一个QT for Android的传感器应用应用程序(摘自笔者2015年将出的《QT5权威指南》,本文为试读篇)
这个手册描述了使用Qt Quick面访的方式在Android和ios设备上开发QtQuick应用程序的方法.我们使用Qt Creator实现一个QtQuick应用程序,这个应用程序基于加速器的值 ...
随机推荐
- 学习:Linux基础知识<一>
>>硬盘分区模式 硬盘分区模式一般如下: -- / (根目录) -- /usr (操作系统) --/home (用户信息) -- /var (默认服务器的登录文件,邮件与WW ...
- jquery ajax 使用layer的超时提示
<!DOCTYPE html> <html> <head> <title>我是标题</title> <meta name=" ...
- php如何修改SESSION的生存时间
如何修改SESSION的生存时间 我们来手动设置 Session 的生存期: <?phpsession_start(); // 保存一天 $lifeTime = 24 * 3600; setco ...
- ACMDP之最长公共子序列长度—HDU1159
Common Subsequence Problem Description A subsequence of a given sequence is the given sequence with ...
- java 大数据处理之内存溢出解决办法(一)
http://my.oschina.net/songhongxu/blog/209951 一.内存溢出类型 1.java.lang.OutOfMemoryError: PermGen space JV ...
- 用WidgeDuino创建一个SCADA(监控与数据採集)系统
WidgeDuino – 近期在Kickstarter上亮相 – 是一个智能的易配置的窗体- 基于Microsoft Windows平台和基于像 Atmel-based Arduino board 的 ...
- HDU 1576 A/B 扩展欧几里德算法
A/B Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- java多线程样例
这里我们做一个完整的样例来说明线程产生的方式不同而生成的线程的差别: package debug; import java.io.*;import java.lang.Thread; class My ...
- mybatis06 增删改差 源码
user.java package cn.itcast.mybatis.po; import java.util.Date; public class User { private int id; p ...
- hibernate相关知识
1.为什么要用Hibernate JDBC的优点 直接底层操作,提供了很简单.便捷的访问数据库的方法,跨平台性比较强.灵活性比较强,可以写很复杂的SQL语句. JDBC的缺点 因为JAVA是面向对象的 ...