Set object to context:

.... QQmlContext * context = new QQmlContext(engine.rootContext()); context->setContextProperty("data", &data); QQmlComponent component(&engine, QUrl(strQmlPath)); .... 

But when used in qml for some reason, it gives the error reference error: data is not defined :

 Item { Connections { target: data onNewMessagePosted: window.visible = false } } 

How to push a c ++ object in qml ?

UPD: the class itself is the simplest example

 class MessageBoard : public QObject { Q_OBJECT public: void callMe () { emit newMessagePosted (); } ; signals: void newMessagePosted (); }; 

    1 answer 1

     ... engine.rootContext()->setContextProperty("data", &data); ... 

    And I would not use the name data , it is used a lot in Qml.

    • It worked!!! Hooray!!! The name is for now as an example. But take note;) - Mira