WorkerScript access to Controller class

我们两清 提交于 2019-12-11 16:57:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!