src/declarative/qml/qdeclarativeworkerscript.cpp
changeset 33 3e2da88830cd
parent 30 5dc02b23752f
child 37 758a864f9613
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
   293         QScriptValue urlContext = workerEngine->newObject();
   293         QScriptValue urlContext = workerEngine->newObject();
   294         urlContext.setData(QScriptValue(workerEngine, fileName));
   294         urlContext.setData(QScriptValue(workerEngine, fileName));
   295         ctxt->pushScope(urlContext);
   295         ctxt->pushScope(urlContext);
   296         ctxt->pushScope(activation);
   296         ctxt->pushScope(activation);
   297         ctxt->setActivationObject(activation);
   297         ctxt->setActivationObject(activation);
       
   298         QDeclarativeScriptParser::extractPragmas(script);
   298 
   299 
   299         workerEngine->baseUrl = url;
   300         workerEngine->baseUrl = url;
   300         workerEngine->evaluate(script);
   301         workerEngine->evaluate(script);
   301 
   302 
   302         workerEngine->popContext();
   303         workerEngine->popContext();
   518     Use WorkerScript to run operations in a new thread.
   519     Use WorkerScript to run operations in a new thread.
   519     This is useful for running operations in the background so
   520     This is useful for running operations in the background so
   520     that the main GUI thread is not blocked.
   521     that the main GUI thread is not blocked.
   521 
   522 
   522     Messages can be passed between the new thread and the parent thread
   523     Messages can be passed between the new thread and the parent thread
   523     using sendMessage() and the onMessage() handler.
   524     using \l sendMessage() and the \l {WorkerScript::onMessage}{onMessage()} handler.
   524 
   525 
   525     Here is an example:
   526     An example:
   526 
   527 
   527     \snippet doc/src/snippets/declarative/workerscript.qml 0
   528     \snippet doc/src/snippets/declarative/workerscript.qml 0
   528 
   529 
   529     The above worker script specifies a javascript file, "script.js", that handles
   530     The above worker script specifies a javascript file, "script.js", that handles
   530     the operations to be performed in the new thread. Here is \c script.js:
   531     the operations to be performed in the new thread. Here is \c script.js:
   538 
   539 
   539     When the user clicks anywhere within the rectangle, \c sendMessage() is
   540     When the user clicks anywhere within the rectangle, \c sendMessage() is
   540     called, triggering the \tt WorkerScript.onMessage() handler in
   541     called, triggering the \tt WorkerScript.onMessage() handler in
   541     \tt script.js. This in turn sends a reply message that is then received
   542     \tt script.js. This in turn sends a reply message that is then received
   542     by the \tt onMessage() handler of \tt myWorker.
   543     by the \tt onMessage() handler of \tt myWorker.
       
   544 
       
   545     \sa {declarative/threading/workerscript}{WorkerScript example},
       
   546         {declarative/threading/threadedlistmodel}{Threaded ListModel example}
   543 */
   547 */
   544 QDeclarativeWorkerScript::QDeclarativeWorkerScript(QObject *parent)
   548 QDeclarativeWorkerScript::QDeclarativeWorkerScript(QObject *parent)
   545 : QObject(parent), m_engine(0), m_scriptId(-1), m_componentComplete(true)
   549 : QObject(parent), m_engine(0), m_scriptId(-1), m_componentComplete(true)
   546 {
   550 {
   547 }
   551 }
   552 }
   556 }
   553 
   557 
   554 /*!
   558 /*!
   555     \qmlproperty url WorkerScript::source
   559     \qmlproperty url WorkerScript::source
   556 
   560 
   557     This holds the url of the javascript file that implements the
   561     This holds the url of the JavaScript file that implements the
   558     \tt WorkerScript.onMessage() handler for threaded operations.
   562     \tt WorkerScript.onMessage() handler for threaded operations.
   559 */
   563 */
   560 QUrl QDeclarativeWorkerScript::source() const
   564 QUrl QDeclarativeWorkerScript::source() const
   561 {
   565 {
   562     return m_source;
   566     return m_source;
   573         m_engine->executeUrl(m_scriptId, m_source);
   577         m_engine->executeUrl(m_scriptId, m_source);
   574 
   578 
   575     emit sourceChanged();
   579     emit sourceChanged();
   576 }
   580 }
   577 
   581 
   578 /*
   582 /*!
   579     \qmlmethod WorkerScript::sendMessage(jsobject message)
   583     \qmlmethod WorkerScript::sendMessage(jsobject message)
   580 
   584 
   581     Sends the given \a message to a worker script handler in another
   585     Sends the given \a message to a worker script handler in another
   582     thread. The other worker script handler can receive this message
   586     thread. The other worker script handler can receive this message
   583     through the onMessage() handler.
   587     through the onMessage() handler.