ReferenceError on context property



  • My application gives me reference error on startup. I'm pretty sure that it is because I load main.qml before defining context property, which is called from qml side on component creation. I'm using qtcharts, which requires the use of QApplication. How ever the application works, but is there a way to get rid of error? I could not figure how to load context property before loading qml file on engine.

    main.cpp

    int main(int argc, char *argv[])
    {
    
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
    
        QQmlComponent component(&engine, QUrl(QLatin1String("qrc:/main.qml")));
    
        QObject *object = component.create();
    
        QScopedPointer<myObject> ilms(new myObject(object, &engine));
        engine.rootContext()->setContextProperty("myObject", myObject.data());
    
        return app.exec();
    }
    

    main.qml

    ComboBox{
             id: dataBox
             objectName: "dataBox"
             model: myObject.data //data is defined Q_PROPERTY in C++ side
             anchors{top: topBar.bottom; left: rectang.left; margins: 10}
    
         }
    


  • Set the context property before creating the QML component.



  • @jpnurmi
    That is what I was asking, how to do it. As you see, the QObject givent to myObject is created from QQmlComponent.



  • QQmlApplicationEngine engine;
    
    // set the context property first
    QScopedPointer<myObject> ilms(new myObject(object, &engine));
    engine.rootContext()->setContextProperty("myObject", myObject.data());
    
    // then load QML that relies on the context property
    QQmlComponent component(&engine, QUrl(QLatin1String("qrc:/main.qml")));
    QObject *object = component.create();
    ...


  • @jpnurmi said in ReferenceError on context property:

    QQmlApplicationEngine engine;
    
    // set the context property first
    QScopedPointer<myObject> ilms(new myObject(object, &engine)); <-----------------------........here
    engine.rootContext()->setContextProperty("myObject", myObject.data());
    
    // then load QML that relies on the context property
    QQmlComponent component(&engine, QUrl(QLatin1String("qrc:/main.qml")));
    QObject *object = component.create();  <---------This is given as argument for the scoped pointer..^
    ...
    

    It does not work, while object is given as argument for myObject



  • Right, I missed than. Well, then you have a circular dependency, a chicken-egg problem, that you have to solve somehow. For example, provide a setter that allows you to set the object after loading QML.

    QQmlApplicationEngine engine;
    
    QScopedPointer<myObject> ilms(new myObject(&engine));
    engine.rootContext()->setContextProperty("myObject", myObject.data());
    
    QQmlComponent component(&engine, QUrl(QLatin1String("qrc:/main.qml")));
    QObject *object = component.create();
    
    ilms->setObject(object);
    

    But this is getting a bit convoluted. Perhaps you could describe the problem and people could give tips how to design a better structure. :) Without knowing any background, I can only throw vague suggestions, but one option could be to register MyObject as a QML type and instantiate it from QML, and connect things using property bindings.


Log in to reply
 

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