在这个样例中,我们将介绍怎样在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);
}
}
}
}
}

在这里,我们使用了一个ListView来显示从worker thread发送来的信息(通过更新model)。这里:

  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="">



整个项目的源代码在:https://github.com/liu-xiao-guo/threading

在QML应用中实现threading多任务的更多相关文章

  1. Python中的threading

    Python中的threading RLock--重入锁 RLock在Python中的实现是对Lock的封装,具体在类中维护了一个重入次数的变量.一旦一个线程获得一个RLock,该线程再次要求获得该锁 ...

  2. Windows Service中使用Threading.Timer需注意回收

    在Windows Service中使用Threading.Timer时需要注意回收池问题 Threading.Timer是基于线程池的,系统会对其进行垃圾回收. 当Threading.Timer定义在 ...

  3. 在Qt示例项目的C ++ / QML源中的//! [0]的含义是什么?

    在Qt示例项目的C ++ / QML源中的//! [0]的含义是什么? 例如:  //! [0] GLWidget :: GLWidget(Helper * helper,QWidget * pare ...

  4. 怎样在QML应用中调用系统设置中的页面来设置我们的系统

    我们在QML应用中有时须要调用系统设置(system settings)来完毕我们的一些设置.比方,我们在使用GPS来定位时,可能GPS并没有打开,假设在我们的应用中直接打开系统中的GPS设置页面,这 ...

  5. 解决QML开发中ComboBox中一个已选择项没有清除的问题

    解决QML开发中ComboBox中一个已选择项没有清除的问题 近期使用QML开发一个项目.须要使用ComboBox进行显示.当进行一个操作时,须要向ComboBox加入一个元素,当进行另外一个操作时. ...

  6. 正确地在QML应用中使用fontsize

    我们知道我们有时须要显示text文本.可是,在QML应用中.我们应该怎样选择font的大小呢?在今天的这篇文章中,我们将展示在Ubuntu平台中的不同文字的大小.我们能够通过FontUtils来帮我们 ...

  7. Qt-QML-关于两个平级的qml文件中的函数调用问题

    这几天还在继续搞我的QML,感悟就QML是坑的同时,也是一门很号的语言,用于快速搭界面是很好的.那么,这几天, 遇到一个问题,在下用一个框框画一下,希望可以理解 抽象派,解释一下,QML1和QML3是 ...

  8. python中的threading模块使用说明

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

  9. 怎样在QML应用中创建一个Context Menu

    我们在非常多的系统中看见能够在屏幕的一个地方长按,然后就能够依据当前显示的上下文弹出一个菜单. 菜单中能够有一些选项,比方删除,改动该项.这样的一般在ListView或GridView中常见.今天,我 ...

随机推荐

  1. mongoDB报错Cannot find module '../build/Release/bson'

    打算用nodejs写一个blog系统,发现nodejs还是存在很多的坑.在使用mongodb时遇到如下报错问题: { [Error: Cannot find module '../build/Rele ...

  2. vue-router路由知识补充

    1.render函数 var app = new Vue({ el: '#app', router, render: h => h(App) //新添加的函数操作 }) 我们新加了render: ...

  3. Volley框架的介绍使用

    Volley是在2013年的Google I/O 2013大会上发布的,是我们的网络通信更快,更简单,更方便.对于初学者来讲是一个很好的框架. 简单来说,它提供了如下的便利功能: JSON,图像等的异 ...

  4. 【系统】supervisor支持多进程

    [program:deployworker] directory = /etc/ansible/easyAnsible/app/deploy/ command = python Deploy.py p ...

  5. SpringApplicationConfiguration 这个不能用 解决方案

    使用的test包的版本号要与spring的一致,避免jar包依赖冲突 直接用注解 @RunWith(SpringRunner.class)@SpringBootTest @SpringApplicat ...

  6. [jQuery] $.map, $.each, detach() , $.getJSOIN()

    $.map function will return the modifies array. $.each function will not new a new array, the old val ...

  7. (算法)Partition方法求数组第k大的数

    如题,下面直接贴出代码: #include <iostream> using namespace std; int Partition(int* A,int left,int right) ...

  8. Linux之nohup命令:实现退出终端后程序继续后台运行

    转自:http://tech.ccidnet.com/art/302/20070618/1115599_1.html 简单而有用的nohup命令在UNIX/LINUX中,普通进程用&符号放到后 ...

  9. 【Javascript Demo】无刷新预览所选择的图片

    1.效果如下,可测试 2.代码如下 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  10. js绝招

    1.JS判断 function IsDigit(cCheck) { //判断是否是数字return (('0'<=cCheck) && (cCheck<='9')); } ...