QQmlContext: Cannot set property on invalid context



  • Hi,

    I keep receiving

    QQmlContext: Cannot set property on invalid context.
    QQmlComponent: Cannot create a component in an invalid context

    when component.create(); in the following code is executed.

    void MainWindow::on_openMapBtn_clicked()
    {
        QQmlEngine engine;
        QMLCommunicationAgent agent;
        engine.rootContext()->setContextProperty("agent", &agent);
        QQmlComponent component(&engine, QUrl::fromLocalFile("map.qml"));
        component.create();
    }
    

    What am I doing wrong?


  • Moderators

    @WhatIf As per the code here the engine and its context object should be valid when the component is being created. I think you can try 2 things:

    • create QQmlEngine and QMLCommunicationAgent on heap using new
    • force QQmlComponent::PreferSynchronous in QQmlComponent

Log in to reply
 

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