Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QQmlApplicationEngine not completely unloading qml components



  • I'm currently loading my main.qml using QQmlApplicationEngine and works fine, and then I want to switch to main2.qml (without calling quit() on my QQmlApplicationEngine since that triggers QCoreApplication::exit() which will exit my application). So I just delete my engine, make a new one and set the context properties again (not the same context properties for main.qml slightly different), and it loads fine. But I get warnings like

    qrc:/qml/...: Unable to assign [undefined] to bool
    

    Why is the engine not unloading and reloading the qml files again? Why do I get a warning for a property that was defined when I loaded the engine the first time but isn't defined when I load the second time.

    NOTE: If I load main2.qml first, I don't get the warning, even though the property is undefined.

    Your help is appreciated.

    EDIT: Here is my code

    QSharedPointer<QQmlApplicationEngine> m_engine;
    QQmlContext* m_ctxt;
    
    void loadEngine(int window){
        m_engine->clearComponentCache();
        m_engine.reset(new QQmlApplicationEngine, &QObject::deleteLater);
        m_ctxt = m_engine->rootContext();
    
        m_ctxt->setParent(m_engine.get());
        QVector<QQmlContext::PropertyPair> qmlProperties;
    
        qmlProperties.push_back(QQmlContext::PropertyPair{"object", QVariant::fromValue(object)});
    
        if(window == 1){
            qmlProperties.push_back(QQmlContext::PropertyPair{"object1", QVariant::fromValue(object1)});
            // add more context properties
    
            m_ctxt->setContextProperties(qmlProperties);
            m_engine->load(QUrl(QLatin1String("qrc:/qml/main.qml")));
        }
        else{    
            qmlProperties.push_back(QQmlContext::PropertyPair{"object2", QVariant::fromValue(object2)});
            // add more context properties
    
            m_ctxt->setContextProperties(qmlProperties);
            m_engine->load(QUrl(QLatin1String("qrc:/qml/main2.qml")));
        }
    }
    


  • @developa said in QQmlApplicationEngine not completely unloading qml components:

    void loadEngine(int window){
    m_engine->clearComponentCache();

    IIRC you should also call trimComponentCache https://doc.qt.io/qt-5/qqmlengine.html#trimComponentCache

    void loadEngine(int window){
        m_engine->clearComponentCache();
        m_engine->trimComponentCache();
    

Log in to reply