Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. ReferenceError on context property
Forum Updated to NodeBB v4.3 + New Features

ReferenceError on context property

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 2 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 5 Offline
    5 Offline
    54474N4
    wrote on last edited by
    #1

    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}
    
         }
    
    1 Reply Last reply
    0
    • jpnurmiJ Offline
      jpnurmiJ Offline
      jpnurmi
      wrote on last edited by
      #2

      Set the context property before creating the QML component.

      5 1 Reply Last reply
      1
      • jpnurmiJ jpnurmi

        Set the context property before creating the QML component.

        5 Offline
        5 Offline
        54474N4
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • jpnurmiJ Offline
          jpnurmiJ Offline
          jpnurmi
          wrote on last edited by
          #4
          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();
          ...
          
          5 1 Reply Last reply
          1
          • jpnurmiJ jpnurmi
            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();
            ...
            
            5 Offline
            5 Offline
            54474N4
            wrote on last edited by
            #5

            @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

            1 Reply Last reply
            0
            • jpnurmiJ Offline
              jpnurmiJ Offline
              jpnurmi
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              2

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved