WorkerScript QML Type

Enables the use of threads in a Qt Quick application. 更多...

导入语句: import QtQml.WorkerScript

特性

信号

方法

详细描述

Use WorkerScript to run operations in a new thread. This is useful for running operations in the background so that the main GUI thread is not blocked.

Messages can be passed between the new thread and the parent thread using sendMessage () 和 onMessage() handler.

An example:

import QtQuick 2.0
Rectangle {
    width: 300; height: 300
    Text {
        id: myText
        text: 'Click anywhere'
    }
    WorkerScript {
        id: myWorker
        source: "script.mjs"
        onMessage: (messageObject)=> myText.text = messageObject.reply
    }
    MouseArea {
        anchors.fill: parent
        onClicked: (mouse)=> myWorker.sendMessage({ 'x': mouse.x, 'y': mouse.y })
    }
}
					

The above worker script specifies a JavaScript file, "script.mjs", that handles the operations to be performed in the new thread. Here is script.mjs :

WorkerScript.onMessage = function(message) {
    // ... long-running operations and calculations are done here
    WorkerScript.sendMessage({ 'reply': 'Mouse is at ' + message.x + ',' + message.y })
}
					

When the user clicks anywhere within the rectangle, sendMessage() is called, triggering the WorkerScript.onMessage() handler in script.mjs . This in turn sends a reply message that is then received by the onMessage() handler of myWorker .

The example uses a script that is an ECMAScript module, because it has the ".mjs" extension. It can use import statements to access functionality from other modules and it is run in JavaScript strict mode.

If a worker script has the extension ".js" instead, then it is considered to contain plain JavaScript statements and it is run in non-strict mode.

注意: Each WorkerScript element will instantiate a separate JavaScript engine to ensure perfect isolation and thread-safety. If the impact of that results in a memory consumption that is too high for your environment, then consider sharing a WorkerScript element.

限定

由于 WorkerScript.onMessage() function is run in a separate thread, the JavaScript file is evaluated in a context separate from the main QML engine. This means that unlike an ordinary JavaScript file that is imported into QML, the script.mjs in the above example cannot access the properties, methods or other attributes of the QML item, nor can it access any context properties set on the QML object through QQmlContext .

Additionally, there are restrictions on the types of values that can be passed to and from the worker script. See the sendMessage () documentation for details.

Worker scripts that are plain JavaScript sources can not use .import syntax. Scripts that are ECMAScript modules can freely use import and export statements.

另请参阅 Qt Quick Examples - Threading and Threaded ListModel Example .

特性文档编制

[read-only] ready : bool

This holds whether the WorkerScript has been initialized and is ready for receiving messages via WorkerScript.sendMessage() .


source : url

This holds the url of the JavaScript file that implements the WorkerScript.onMessage() handler for threaded operations.

If the file name component of the url ends with ".mjs", then the script is parsed as an ECMAScript module and run in strict mode. Otherwise it is considered to be plain script.


信号文档编制

message ( jsobject msg )

This signal is emitted when a message msg is received from a worker script in another thread through a call to sendMessage ().

注意: 相应处理程序是 onMessage .

方法文档编制

sendMessage ( jsobject message )

发送给定 message to a worker script handler in another thread. The other worker script handler can receive this message through the onMessage() handler.

The message object may only contain values of the following types:

  • boolean, number, string
  • JavaScript objects and arrays
  • ListModel objects (any other type of QObject * is not allowed)

All objects and arrays are copied to the message . With the exception of ListModel objects, any modifications by the other thread to an object passed in message will not be reflected in the original object.