gpt4 book ai didi

multithreading - QML:在并行线程中运行函数

转载 作者:行者123 更新时间:2023-12-04 06:49:53 25 4
gpt4 key购买 nike

在我的代码中,我正在循环创建 16x16 按钮,这需要几秒钟。

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);
}

当创建按钮的功能运行时,应用程序卡住。我想在此功能运行时显示加载动画。那么,如何在并行线程中运行此函数以避免卡住?

最佳答案

要在线程中工作,您有两种可能的方法:

  • 阅读 WorkerScript元素。它允许您通过将 javascript 函数作为线程运行来执行某些操作。

  • 注:正如文档中给出的那样,虽然有一个限制:

    Since the WorkerScript.onMessage() function is run in a separate thread, the JavaScript file is evaluated in a context separate from the main QML engine. This means that unlike an ordinary JavaScript file that is imported into QML, the script.js in the above example cannot access the properties, methods or other attributes of the QML item, nor can it access any context properties set on the QML object through QDeclarativeContext. Additionally, there are restrictions on the types of values that can be passed to and from the worker script. See the sendMessage() documentation for details.



    看看,如果对于您的特定用例,它是否符合要求。

    2
    .实现像 C++ 线程一样繁重的功能。每当需要时,生成一个信号以在 C++ 端启动此线程。完成后,如果需要,将数据从 C++ 传回 Qml 。

    关于multithreading - QML:在并行线程中运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16025478/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com