在 QT Qml 中动态创建导致冻结且不响应的对象

Dynamically creating object causing freezing and not responding in QT Qml

我必须调用一个繁重的 qml 文件,但是当我在文档中使用 Qt 给出的代码时,我的应用程序会冻结并且还会进入不响应状态几秒钟。

 property var component;
function createSpriteObjects() {

    component = Qt.createComponent("qrc:/qml/called.qml");
  
    if (component.status === Component.Ready)
      {  
        finishCreation();
    }
    else {
        component.statusChanged.connect(finishCreation);
    }
}

function finishCreation() {
   
    if (component.status === Component.Ready) {
    
        var sprite = component.createObject(callingwindow);
       
        if (sprite === null) {
            // Error Handling
            console.log("Error creating object");
        }
    } else if (component.status === Component.Error) {
        // Error Handling
        console.log("Error loading component:", component.errorString());
    }
}

使用QCoreApplication::processEvents();

事件循环从调用 exec() 开始。 Long-运行 操作可以调用 processEvents() 以保持应用程序响应。