Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

QML WorkerScript Element

The WorkerScript element enables the use of threads in QML. More...

Properties

Signal Handlers

Methods

Detailed Description

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() and the onMessage() handler.

An example:

 import QtQuick 1.0

 Rectangle {
     width: 300; height: 300

     Text {
         id: myText
         text: 'Click anywhere'
     }

     WorkerScript {
         id: myWorker
         source: "script.js"

         onMessage: myText.text = messageObject.reply
     }

     MouseArea {
         anchors.fill: parent
         onClicked: myWorker.sendMessage({ 'x': mouse.x, 'y': mouse.y })
     }
 }

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

 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.js. This in turn sends a reply message that is then received by the onMessage() handler of myWorker.

Restrictions

Since the 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.js 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 QDeclarativeContext.

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.

See also WorkerScript example and Threaded ListModel example.

Property Documentation

source : url

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


Signal Handler Documentation

WorkerScript::onMessage ( jsobject msg )

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


Method Documentation

WorkerScript::sendMessage ( jsobject message )

Sends the given 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.


Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.8
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web