在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中常见.今天,我 ...
随机推荐
- GoLang中面向对象的三大特性
有过 JAVA 语言学习经历的朋友都知道,面向对象主要包括了三个基本特征:封装.继承和多态.封装,就是指运行的数据和函数绑定在一起,JAVA 中主要是通过 super 指针来完成的:继承,就是指 cl ...
- 【RPC】Thrift ICE 等 RPC 框架相关资料
RPC框架-Thrift-ICE Apache Thrift - Documentation Apache Thrift - Index of tutorial/ Apache Thrift - Ab ...
- iOS_2_button控制物体形变
终于效果图: BeyondViewController.h // // BeyondViewController.h // 02_button控制物体形变 // // Created by beyon ...
- WIN32 SDK对COM的支持
- [Backbone]5. Model & View, toggle between Models and Views -- 2
Dr. Goodparts is pretty flaky and has been cancelling a lot of appointments lately. He's asked for a ...
- 输入框提示文字跨浏览器的placeholder-jQuery版
<script type="text/javascript" src="jquery-1.7.2.min.js"></script> & ...
- Android 四大组件之 Activity(一)
1.Activity的定义及作用: Android系统中的四大组件之一,可以用于显示View.Activity是一个与用户交互的系统模块,几乎所有的Activity都是和用户进行交互的一个应用程序的组 ...
- Eclipse中SVN修改的*星号没了,解决方法
Eclipse中SVN修改的*星号没了,解决方法 打开Preference 第一步:去掉外加的 ">" 第二步:勾选Outgoing changes 这样做之后," ...
- JAVA 爬虫Gecco
主要代码: Gecco(matchUrl="https://github.com/{user}/{project}", pipelines="consolePipelin ...
- mac系统下ionic环境配置
本人是在mac环境下进行配置的: 下载nodejs:https://nodejs.org/download/ 并双击安装 Cordova and Ionic command-line tools 安装 ...