c++ - WorkerScript access to Controller class -


i have busyindicator should spin while heavy computations happening , stop when computations done.

i thought workerscript right way go but from here, seems secondary (computation thread) in .js file not have access objects of primary .qml thread.

this problematic computations performed through controller c++ defined qobject instantiated primary thread.

here 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 [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 not defined in importscanworkerscript.js.

should understand workerscript can handle simple situations?

as noticed, workerscript cannot access ui controls. separate thread can "talk" main ui thread using messages. me works in other languages/frameworks. send message thread whenever want update ui or object. example:

workerscript.onmessage = function(message) {     workerscript.sendmessage({ 'action': 'start' })     // heavy task here     workerscript.sendmessage({ 'action': 'finish', 'data': somedata }) } 

and main qml may 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;         }     } } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -