QMLアプリケーションでのthreadingマルチタスクの実装
この例では、QMLアプリケーションでQML言語で提供されるthreading機能を使用して、マルチタスクを実現する方法について説明します.詳細は次のとおりです.http://doc.qt.io/qt-5/qtquick-threading-example.html
Ubuntu SDKを使用して、最も基本的なQMLプロジェクトを作成します.
Main.qml
ここでは、worker threadから送信された情報を表示するためにListViewを使用します(modelを更新します).ここ:
WorkerScriptはworker threadを定義します.その実行コードはdataloaderにあります.js:
dataloader.js
Mainでqmlでは、2秒ごとにworker threadにリクエストを送信するTimerを定義します.パラメータは、次のように定義されたobjectです.
work threadで送信された情報を受信した後、上のコードでmsgのactionをチェックし、受信したmodelを同時に更新します.
我々のルーチンを実行します.
プロジェクト全体のソースコードは次のとおりです.https://github.com/liu-xiao-guo/threading
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);
}
}
}
}
}
ここでは、worker threadから送信された情報を表示するためにListViewを使用します(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では、2秒ごとにworker threadにリクエストを送信するTimerを定義します.パラメータは、次のように定義されたobjectです.
{'action': 'appendCurrentTime', 'model': listModel};
work threadで送信された情報を受信した後、上のコードでmsgのactionをチェックし、受信したmodelを同時に更新します.
我々のルーチンを実行します.
プロジェクト全体のソースコードは次のとおりです.https://github.com/liu-xiao-guo/threading