标签:
在这个例子中,我们将介绍如何在QML应用中使用QML语言提供的threading功能,实现多任务。更多的阅读在:http://doc.qt.io/qt-5/qtquick-threading-example.html
我们使用Ubuntu SDK来创建以个最基本的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" }
// ![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]
{'action': 'appendCurrentTime', 'model': listModel};
标签:
原文地址:http://blog.csdn.net/ubuntutouch/article/details/46426733