QtQuick dynamic object in a different context

為{幸葍}努か 提交于 2019-12-06 11:53:32

I have a workable solution. Instead of trying to load the qml inline the loader item can be used to dynamically manage items.

Here's code to load an item in response to a mouse click:

MouseArea
{
    anchors.fill: parent
    function changePlugin()
    {
        // unload previously loaded plugin
        pluginLoader.sourceComponent = undefined;
        // load new plugin
        pluginLoader.sourceComponent = myPlugin;
    }
    onClicked: changePlugin()
}

Insert a definition of what you want to load, in the spot where you want to load it:

Component
{
    id: myPlugin
    YourCustomPlugin
    {
        // do initialization when the object is loaded
        // I call the init method of my plugin
        Component.onCompleted: init();
    }
}

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