QML: working functions in parallel threads

In my code, I create 16x16 buttons in a loop, and it takes a few seconds.

onCreateField:
{
    for(var i=0;i<fieldWidth;i++)
    {
        for(var j=0;j<fieldHeight;j++)
        {
            createButton(i, j);
        }
    }
}

function createButton(x, y)
{
    __buttonX = x;
    __buttonY = y;

    __component = Qt.createComponent("GameButton.qml");

    if(__component != null)
        continueButtonCreation();
    else
        __component.ready.connect(continueButtonCreation);
}

function continueButtonCreation()
{
    var button = __component.createObject(field, {"row": __buttonY, "column": __buttonX});

     if (button == null) {
         // Error Handling
         console.log("Error creating object");

         return;
     }

     updateValveState.connect(button.stateUpdated);
     button.buttonClicked.connect(buttonClicked);

     field.clearField.connect(button.release);
}

While the button creation function is working, the application freezes. I want to show loading animation while this function is running. So, how to run this function in a parallel thread to avoid freezing?

+5
source share
3 answers

To work in threads, you have two possible approaches:

  • Learn more about WorkerScript . It allows you to perform a specific operation by running javascript functions as threads.

Note: As indicated in the documentation, there is a limitation:

WorkerScript.onMessage() thread, JavaScript QML. , JavaScript , QML, script.js , QML , , QML QDeclarativeContext. , , script. . sendMessage().

, .

2 , , ++. , ++. , ++ Qml, .

+10

, QML javascript . , , GridView , . javascript.

+1

  • incubateObject createObject
  • (16 ) :
+1

All Articles