在QML应用中实现threading多任务
在这个样例中,我们将介绍怎样在QML应用中使用QML语言提供的threading功能,实现多任务。
很多其它的阅读在:http://doc.qt.io/qt-5/qtquick-threading-example.html
我们使用Ubuntu SDK来创建以个最主要的QML项目:
Main.qml
import QtQuick 2.0
import Ubuntu.Components 1.1 /*!
\brief MainView with a Label and Button elements.
*/ MainView {
// objectName for functional testing purposes (autopilot-qt5)
objectName: "mainView" // Note! applicationName needs to match the "name" field of the click manifest
applicationName: "threading.liu-xiao-guo" /*
This property enables the application to change orientation
when the device is rotated. The default is false.
*/
//automaticOrientation: true // Removes the old toolbar and enables new features of the new header.
useDeprecatedToolbar: false width: units.gu(60)
height: units.gu(85) Page {
title: i18n.tr("threading") ListView {
anchors.fill: parent
model: listModel
delegate: Component {
Text { text: time }
} ListModel { id: listModel } WorkerScript {
id: worker
source: "dataloader.js"
} Timer {
id: timer
interval: 2000; repeat: true
running: true
triggeredOnStart: true onTriggered: {
var msg = {'action': 'appendCurrentTime', 'model': listModel};
worker.sendMessage(msg);
}
}
}
}
}
WorkerScript {
id: worker
source: "dataloader.js"
}
WorkerScript定义了一个worker thread。它的运行代码在dataloader.js中:
dataloader.js
// ![0]
WorkerScript.onMessage = function(msg) {
if (msg.action == 'appendCurrentTime') {
var data = {'time': new Date().toTimeString()};
msg.model.append(data);
msg.model.sync(); // updates the changes to the list
}
}
// ![0]
在Main.qml中,我们定义了一个Timer,每2秒发送一个请求给worker thread。
它的參数是一个例如以下定义的object:
{'action': 'appendCurrentTime', 'model': listModel};
在work thread接受到发送来的信息后。在上面的代码中。检查msg中的action。并同一时候更新传入的model。
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvVWJ1bnR1VG91Y2g=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="200" height="300" alt="">
在QML应用中实现threading多任务的更多相关文章
- Python中的threading
Python中的threading RLock--重入锁 RLock在Python中的实现是对Lock的封装,具体在类中维护了一个重入次数的变量.一旦一个线程获得一个RLock,该线程再次要求获得该锁 ...
- Windows Service中使用Threading.Timer需注意回收
在Windows Service中使用Threading.Timer时需要注意回收池问题 Threading.Timer是基于线程池的,系统会对其进行垃圾回收. 当Threading.Timer定义在 ...
- 在Qt示例项目的C ++ / QML源中的//! [0]的含义是什么?
在Qt示例项目的C ++ / QML源中的//! [0]的含义是什么? 例如: //! [0] GLWidget :: GLWidget(Helper * helper,QWidget * pare ...
- 怎样在QML应用中调用系统设置中的页面来设置我们的系统
我们在QML应用中有时须要调用系统设置(system settings)来完毕我们的一些设置.比方,我们在使用GPS来定位时,可能GPS并没有打开,假设在我们的应用中直接打开系统中的GPS设置页面,这 ...
- 解决QML开发中ComboBox中一个已选择项没有清除的问题
解决QML开发中ComboBox中一个已选择项没有清除的问题 近期使用QML开发一个项目.须要使用ComboBox进行显示.当进行一个操作时,须要向ComboBox加入一个元素,当进行另外一个操作时. ...
- 正确地在QML应用中使用fontsize
我们知道我们有时须要显示text文本.可是,在QML应用中.我们应该怎样选择font的大小呢?在今天的这篇文章中,我们将展示在Ubuntu平台中的不同文字的大小.我们能够通过FontUtils来帮我们 ...
- Qt-QML-关于两个平级的qml文件中的函数调用问题
这几天还在继续搞我的QML,感悟就QML是坑的同时,也是一门很号的语言,用于快速搭界面是很好的.那么,这几天, 遇到一个问题,在下用一个框框画一下,希望可以理解 抽象派,解释一下,QML1和QML3是 ...
- python中的threading模块使用说明
这段时间使用python做串口的底层库,用到了多线程,对这部分做一下总结.实际用完了后再回过头去看python的官方帮助文档,感觉受益匪浅,把里面的自己觉得有用的一些关键点翻译出来,留待后续查验.th ...
- 怎样在QML应用中创建一个Context Menu
我们在非常多的系统中看见能够在屏幕的一个地方长按,然后就能够依据当前显示的上下文弹出一个菜单. 菜单中能够有一些选项,比方删除,改动该项.这样的一般在ListView或GridView中常见.今天,我 ...
随机推荐
- Eclipse 创建文件快捷菜单、避免格式化时自动换行、.properties文件中文乱码、在线安装FreeMarker
创建文件快捷菜单设置 打开窗口“Customize Perspective - Java EE”,切换选项卡到“Shortcuts”: 进行一下配置: “Generate”:如上图勾选方式 " ...
- [HTML5] Avoiding CSS Conflicts via Shadow DOM CSS encapsulation
Shadow DOM is part of the web components specification. It allows us to ship self contained componen ...
- 没有main函数的helloworld
差点儿全部程序猿的第一堂课都是学习helloworld程序,以下我们先来重温一下经典的C语言helloworl /* hello.c */ #include <stdio.h> int m ...
- (算法)位图BitMap
题目: 给定一数组,大小为M,数组中的数字范围为1-N,如果某带宽有限,无法传输该大小的数组,该怎么办? 思路: 通过位图BitMap来压缩数组,将数组中每个数字在bit位上标志,这样就可以将数组大小 ...
- hdu1874 畅通project续(求最短路径)
畅通project续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 关于LayoutInflater的错误用法
转自:http://www.doubleencore.com/2013/05/layout-inflation-as-intended/ Layout inflation is the term us ...
- 字符串的公共前缀对Mysql B+树查询影响回溯分析
年前项目组接微信公众号. 上线之后,跟微信相关的用cid列的查询会话的SQL变慢了几十倍!思考这个问题思考了非常久.从出现以来一直是我心头的一个结.cid这一列是建了索引的,普通的cid列更新 ...
- UISlider设置按钮透明
UISlider *aslider = [[UISlider alloc]initWithFrame:kCR(, , , )]; [aslider setValue:0.5]; [aslider se ...
- Element学习
生成 HTML 文档初始结构 HTML 文档的初始结构,就是包括 doctype.html.head.body 以及 meta 等内容.你只需要输入一个 “!” 就可以生成一个 HTML5 的标准文档 ...
- javascript 1.5s跳转
<script type="text/javascript"> var t = 1.5; window.onload=countDown; function count ...