在这个样例中,我们将介绍怎样在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 manifestapplicationName: "threading.liu-xiao-guo"/*This property enables the application to change orientationwhen the device is rotated. The default is false.*///automaticOrientation: true// Removes the old toolbar and enables new features of the new header.useDeprecatedToolbar: falsewidth: units.gu(60)height: units.gu(85)Page {title: i18n.tr("threading")ListView {anchors.fill: parentmodel: listModeldelegate: Component {Text { text: time }}ListModel { id: listModel }WorkerScript {id: workersource: "dataloader.js"}Timer {id: timerinterval: 2000; repeat: truerunning: truetriggeredOnStart: trueonTriggered: {var msg = {'action': 'appendCurrentTime', 'model': listModel};worker.sendMessage(msg);}}}}
}

在这里,我们使用了一个ListView来显示从worker thread发送来的信息(通过更新model)。这里:
  WorkerScript {id: workersource: "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。

执行我们的例程:
整个项目的源代码在:https://github.com/liu-xiao-guo/threading

转载于:https://www.cnblogs.com/yutingliuyl/p/6705699.html

在QML应用中实现threading多任务相关推荐

  1. QML Map中测距——QtLocation轻量级地图应用学习

    QML Map中测距 1. 实现思路 2. 实现代码及git链接 本文转载于:QML QtLocation轻量级地图应用学习:实现测距功能 所有的热爱都要不遗余力,真正喜欢它便给它更高的优先级,和更多 ...

  2. 如何在Ubuntu QML应用中使用Push Notification

    我们知道目前Ubuntu手机平台有些类似iPhone平台,是一个单任务的操作系统,虽然系统本身具有多任务的功能.如果当前的应用被推到后台的话,应用将会被自动挂起,而不会被系统所运行.在这个时候如果我们 ...

  3. python中的threading_python中的threading模块使用说明

    这段时间使用python做串口的底层库,用到了多线程,对这部分做一下总结.实际用完了后再回过头去看python的官方帮助文档,感觉受益匪浅,把里面的自己觉得有用的一些关键点翻译出来,留待后续查验. t ...

  4. python的threading是什么意思_Python中的threading模块是什么

    threading提供了一个比thread模块更高层的API来提供线程的并发性.这些线程并发运行并共享内存. 下面来看threading模块的具体用法: 一.Thread的使用,目标函数可以实例化一个 ...

  5. QML delegate中使用Repeater

    在QML界面设计例如ListView中,通过设计delegate来显示代理model中的数据,在delegate中使用属性绑定的方式将model中对象的值显示出来,但如果delegate中使用了Rep ...

  6. QML地图中使用MapItemView

    在QML地图上,我们可以自定义的画圆,画方,画线等等.当我们需要有较多的数据需要在地图上展示时,通过动态新增的方式很明显不利于管理和维护,因此用到MapItemView. MapItemView用于展 ...

  7. DSP/BIOS实时操作系统中PIP对象多任务间传递数据示例程序

    2019独角兽企业重金招聘Python工程师标准>>> #include <std.h>#include <log.h> #include <tsk.h ...

  8. 深度学习核心技术精讲100篇(八)-keras 实战系列之深度学习中的多任务学习(Multi-task learning)

    多任务学习(Multi-task learning)简介 多任务学习(Multi-task learning)是迁移学习(Transfer Learning)的一种,而迁移学习指的是将从源领域的知识( ...

  9. QML中定义JavaScript资源

    QML中定义JavaScript资源 QML中定义JavaScript资源 共享的JavaScript资源(库) QML中定义JavaScript资源 QML应用程序的程序逻辑可以在JavaScrip ...

最新文章

  1. GitHub 又一开源神器!写代码、搜问题,全部都在「终端」完成!
  2. ASP.NET存取图片到数据库
  3. CSS 颜色 表示方法
  4. 《TOMCAT权威指南》摘抄
  5. 【转】DevOps到底是什么意思?
  6. 实现了HTTP多线程下载
  7. bzoj 2245 [SDOI2011]工作安排【最小费用最大流】
  8. 大四课程设计之基于RFID技术的考勤管理系统(一)项目介绍
  9. Rsyslog+LogAnalyzer+MySQL部署日志服务器
  10. SQLAlchemy Mapping Table Columns
  11. Azure Data Studio:Postgres 专用的超强开源 GUI 编辑器了解一下?
  12. 江西工业职业技术学院计算机专业在哪个校区,江西工业职业技术学院几个校区...
  13. 确认从博客园搬家到FPA博客园
  14. delphi random_delphi产生随机数
  15. selenium模拟鼠标滑动
  16. 基于USB数据采集卡(DAQ)与IO模块的热电偶温度采集
  17. 按字母顺序排列c语言,C语言有n个国家名,要求按字母先后顺序排列,并按升序输出。(要求:尽量使用标准字符串函数)...
  18. 中文不能输入超过20,英文输入不能超过40个
  19. 详解oracle多种表连接方式,详解Oracle多种表连接方式
  20. 实现语音对讲_校园IP广播对讲融合解决方案

热门文章

  1. SpringCloud 从菜鸟到大牛之六 消息和异步 MQ
  2. 【实战】docker-compose 编排 多个docker 组成一个集群并做负载
  3. 从源码分析RocketMQ系列-Producer的invokeSync()方法
  4. Python精通-运算符与基本数据类型(一)
  5. Json解析工具Jackson(简单应用)
  6. 教你手工检测SQL注入
  7. mwget安装及使用
  8. 分享一位电商大佬的技术笔记
  9. MYSQL统计行数时到底应该怎么COUNT
  10. Springboot 整合jsp案例