Qml workerscript Element
The workerscript element enables the use of threads in qml
Source: URL
Signals: onmessage
Methods: sendmessage
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.
Workerscript is used to generate a new thread and communicate with each other through messages.
Declare workerscript {ID: worker; Source: "dataloader. js"} ID and JS file path in the qml file. In this way, you can use worker in qml. sendmessage (MSG); send a message (MSG is a var variable defined in qml). In JS, workerscript is always defined in this way. onmessage = function (MSG) function, where MSG can contain multiple variables or attributes in worker MSG. Similarly, JS can also send messages to qml through workerscript. sendmessage ({SRC: SRC, data: jresult}), where onmessage in qml is responsible for receiving messages.
Note:
There are no global variables in the workerscript thread, so the following usage will occur in MSG. model. append (string);, causing thread crash.
VaR string = "time :";
Workerscript. onmessage = function (fmurl ){
If (msg. Action = 'appendcurrenttime '){
VaR DATA = {'time': new date (). totimestring ()};
MSG. model. append (string );
MSG. model. append (data );
MSG. model. Sync (); // updates the changes to the list
}
}