Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Solved QQmlContext::setContextProperty: property is not defined.

    QML and Qt Quick
    6
    7
    8104
    Loading More Posts
    • 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.
    • I
      Ibrahim last edited by

      Hi;

      // main.cpp
      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <QQmlContext>
      
      int main(int argc, char* argv[])
      {
        QGuiApplication app(argc, argv);
      
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        QQmlContext* context = new QQmlContext(engine.rootContext());
        context->setContextProperty("num", 77);
      
        return app.exec();
      }
      
      // MainForm.ui.qml
      import QtQuick 2.5
      
      Rectangle {
        width: 360
        height: 360
        property alias text0: text0.text
        Text {
          id: text0
          x: parent.width / 2
          y: parent.height / 2
        }
      }
      // main.qml
      import QtQuick 2.5
      import QtQuick.Window 2.2
      
      Window {
        visible: true
      
        MainForm {
          anchors.fill: parent
          text0: num
        }
      }
      

      This code is getting this error message: qrc:/main.qml:9: ReferenceError: num is not defined but I defined num property in main.cpp. What is the problem? Thanks.

      1 Reply Last reply Reply Quote 0
      • Charby
        Charby last edited by

        The problem is that you don't provide the property to the root context but to a copy...
        you should rather use this :

        QQmlContext* context = engine.rootContext();
        context->setContextProperty("num", 77);
        //engine.rootContext()->setContextProperty("num", 77); //even simpler
        
        I zeFreelance 2 Replies Last reply Reply Quote 2
        • I
          Ibrahim @Charby last edited by

          @Charby Thanks. The problem solved.

          1 Reply Last reply Reply Quote 0
          • jpnurmi
            jpnurmi last edited by

            Context properties must be set before loading QML that references the context properties.

            1 Reply Last reply Reply Quote 1
            • zeFreelance
              zeFreelance @Charby last edited by

              I copied the (non-working) code from a Qt 5.12.3 help page.
              I am not sure why they put it in examples if that code doesn't work. :(
              @Charby your code works seamlessly. Thanks a ton for clarifying this to all of us!

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                Hi @zeFreelance and welcome to devnet,

                What example would that be ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 0
                • fcarney
                  fcarney last edited by

                  @SGaist said in QQmlContext::setContextProperty: property is not defined.:

                  What example would that be ?

                  I have seen this bad code and copy and pasted it myself without realizing I was creating a new context object. It was on stackoverflow if I remember right. I don't think I have seen this as an example on Qt docs pages though.

                  C++ is a perfectly valid school of magic.

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post