问题
I have a BusyIndicator which should spin while heavy computations are happening and stop when the computations are done.
I thought WorkerScript was the right way to go but but from here, it seems that the secondary (computation thread) in the .js file does not have access to the objects of the primary .qml thread.
This is problematic as all my computations are performed through a Controller C++ defined QObject instantiated by the primary thread.
Here is my code:
main.qml
import QtQuick 2.7
import QtQuick.Layouts 1.3
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import QtQuick.Controls.Material 2.0
import QtQuick.Controls.Styles 1.2
import QtQuick.Dialogs 1.2
import LcQml 1.0
ApplicationWindow
{
id: window
UiController
{
id: uiController
}
WorkerScript
{
id: importScanWorkerScript
source: "importScanWorkerScript.js"
onMessage:
{
busyIndicator.running = false;
}
}
FileDialog
{
id: importScanDialog
visible: false
title: "Import a [scan] file"
folder: "MyScannedScenesFolder"
nameFilters: [ "STL files (*stl)" ]
selectedNameFilter: "STL files (*stl)"
onAccepted:
{
importScanWorkerScript.sendMessage({'filepath': importScanDialog.fileUrl})
busyIndicator.running = true;
}
}
BusyIndicator
{
id: busyIndicator
running: false
anchors.centerIn: parent
}
}
importScanWorkerScript.js
WorkerScript.onMessage = function(message)
{
uiController.onImportScanDevMenuClicked(message.filepath);
WorkerScript.sendMessage()
}
Pb: uiController is not defined in importScanWorkerScript.js.
Should I understand that WorkerScript can only handle simple situations?
回答1:
As you already noticed, WorkerScript
cannot access UI controls. But your separate thread can "talk" to the main UI thread using messages. As for me that works exactly as in all other languages/frameworks. Just send a message from the thread whenever you want to update UI or your object.
For example:
WorkerScript.onMessage = function(message) {
WorkerScript.sendMessage({ 'action': 'start' })
// do some heavy task here
WorkerScript.sendMessage({ 'action': 'finish', 'data': somedata })
}
and so your main qml may look like this:
WorkerScript {
id: myWorker
source: "func.js"
onMessage: {
switch(messageObject.action)
{
case 'start':
spinner.running = true;
uiController.doSomething();
break;
case 'finish':
spinner.running = false;
uiController.doSomethingAnother();
break;
}
}
}
来源:https://stackoverflow.com/questions/45752428/workerscript-access-to-controller-class