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. setContextProperty not setting context properly

setContextProperty not setting context properly

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.5k 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #1

    Okay, the way I understand it, I have to set the context properties before I load a qml file. So I did this:

    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        //QQuickView view(QUrl(QStringLiteral("qrc:/main.qml")));
        //QQuickItem *item = view.rootObject();
    
        Controller control; // qobject based
    
        QQmlApplicationEngine engine;
    
        QQmlContext *context = new QQmlContext(engine.rootContext());
        context->setContextProperty("controller", &control);
    
        //engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        //engine.load(QUrl());
        //if (engine.rootObjects().isEmpty())
        //    return -1;
        QQmlComponent component(&engine);
        component.loadUrl(QUrl(QStringLiteral("qrc:/main.qml")));
    
        QQuickItem *tobj;
    
        if (component.isReady()){
            QObject *myObject = component.create();
            tobj = qobject_cast<QQuickItem*>(myObject);
        }else
            qWarning() << component.errorString();
    
        //QQuickItem *item = engine.rootObjects();
        for(auto item: engine.rootObjects())
        {
            QObject::connect(item, SIGNAL(operateSig()),
                                 &control, SIGNAL(operate()));
            break;
        }
    
        return app.exec();
    }
    

    QML file:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Controls 1.4
    
    /*
      This program is for testing out OPC UA interfaces.
      */
    
    Window {
        id: mainWindow
        visible: true
        width: 640
        height: 480
        title: qsTr("OPC UA Playground")
    
        signal operateSig()
    
        Connections {
            target: controller
            onStatus: {
                console.log(message);
            }
        }
    
        Button {
            id: operateButton
            text: "Operate"
            onClicked: {
                mainWindow.operateSig();
            }
        }
    
    }
    

    However, I get these errors:

    qrc:/main.qml:18:5: QML Connections: Cannot assign to non-existent property "onStatus"
    qrc:/main.qml:19: ReferenceError: controller is not defined
    

    I don't understand why it cannot see the "controller" object. I have set contexts before in other apps and it has worked. I have to be missing something really really simple.

    C++ is a perfectly valid school of magic.

    sierdzioS 1 Reply Last reply
    0
    • fcarneyF fcarney

      Okay, the way I understand it, I have to set the context properties before I load a qml file. So I did this:

      int main(int argc, char *argv[])
      {
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      
          QGuiApplication app(argc, argv);
      
          //QQuickView view(QUrl(QStringLiteral("qrc:/main.qml")));
          //QQuickItem *item = view.rootObject();
      
          Controller control; // qobject based
      
          QQmlApplicationEngine engine;
      
          QQmlContext *context = new QQmlContext(engine.rootContext());
          context->setContextProperty("controller", &control);
      
          //engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
          //engine.load(QUrl());
          //if (engine.rootObjects().isEmpty())
          //    return -1;
          QQmlComponent component(&engine);
          component.loadUrl(QUrl(QStringLiteral("qrc:/main.qml")));
      
          QQuickItem *tobj;
      
          if (component.isReady()){
              QObject *myObject = component.create();
              tobj = qobject_cast<QQuickItem*>(myObject);
          }else
              qWarning() << component.errorString();
      
          //QQuickItem *item = engine.rootObjects();
          for(auto item: engine.rootObjects())
          {
              QObject::connect(item, SIGNAL(operateSig()),
                                   &control, SIGNAL(operate()));
              break;
          }
      
          return app.exec();
      }
      

      QML file:

      import QtQuick 2.12
      import QtQuick.Window 2.12
      import QtQuick.Controls 1.4
      
      /*
        This program is for testing out OPC UA interfaces.
        */
      
      Window {
          id: mainWindow
          visible: true
          width: 640
          height: 480
          title: qsTr("OPC UA Playground")
      
          signal operateSig()
      
          Connections {
              target: controller
              onStatus: {
                  console.log(message);
              }
          }
      
          Button {
              id: operateButton
              text: "Operate"
              onClicked: {
                  mainWindow.operateSig();
              }
          }
      
      }
      

      However, I get these errors:

      qrc:/main.qml:18:5: QML Connections: Cannot assign to non-existent property "onStatus"
      qrc:/main.qml:19: ReferenceError: controller is not defined
      

      I don't understand why it cannot see the "controller" object. I have set contexts before in other apps and it has worked. I have to be missing something really really simple.

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @fcarney said in setContextProperty not setting context properly:

      QQmlContext *context = new QQmlContext(engine.rootContext());

      You are constructing a new context, which is not the same as the one your engine is running on.

      Do this instead:

      QQmlContext *context = engine.rootContext();
      context->setContextProperty("controller", &control);
      

      Or simpler:

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

      And then engine.load("qrc:/main.qml"); of course.

      (Z(:^

      1 Reply Last reply
      2
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #3

        @sierdzio said in setContextProperty not setting context properly:

        You are constructing a new context, which is not the same as the one your engine is running on.

        Thank you! I got that from too much copy pasta trying to figure out how to do this. All my errors are gone now.

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        0

        • Login

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