Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Dynamically loading and unloading a QML file
In my Qt code I am loading a QML file like this m_view->setSource(QUrl("qrc:///main.qml"));
*main.qml is my main view. On the click of a button I need to reset the source and load a new QML file
something like m_view->setSource(QUrl("qrc:///numbers.qml")); which will have different QML components.
is there a way where I can unload the previous QML file and load the new one. Due to some design constraints I can have only one view at a time.
Use Loader element in QML. If you want to do it as you describe, you probably need to close the window, delete your QQuickView and then set it up again from scratch. IMO, Loader is much better here.
monsingh last edited by
this is applicable for qml in QT ie; qml is incorparated with Qt;
you can subclassing QDeclarativeView api in your class.
based on the event change, you can change the qml dynamically.
this will works
Is there a way to unload the previously loaded view ? Some thing like this
So that I can unload the previously loaded qml file and load the new one.
If I am use the loader still there will be 2 views at a time. Unfortunately I can have only one view at a time .
No. Open a single QML file that contains only the loader element. Then you load your main.qml there. When you need to switch, you change the source of the Loader to numbers.qml and you're all set. Single window.