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 contextwhen
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?
-
@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
andQMLCommunicationAgent
on heap usingnew
- force
QQmlComponent::PreferSynchronous
inQQmlComponent
- create