Dynamically loading and unloading a QML file



  • Hi ,
    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.


  • Moderators

    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.



  • Hi

    this is applicable for qml in QT ie; qml is incorparated with Qt;

    you can subclassing QDeclarativeView api in your class.

    Class1

    setSource(QUrl("qrc:/qml/Main.qml"));

    Class2

    setSource(QUrl("qrc:/qml/Sub.qml"));

    based on the event change, you can change the qml dynamically.

    this will works



  • Hi monsingh,
    Is there a way to unload the previously loaded view ? Some thing like this
    m_view->resetSource()
    So that I can unload the previously loaded qml file and load the new one.



  • Hi sierdzio,
    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 .


  • Moderators

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.