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. QQmlContext::setContextProperty: property is not defined.

QQmlContext::setContextProperty: property is not defined.

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 6 Posters 9.3k Views
  • 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 Offline
    I Offline
    Ibrahim
    wrote on last edited by
    #1

    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
    0
    • CharbyC Offline
      CharbyC Offline
      Charby
      wrote on last edited by
      #2

      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 zeFreelanceZ 2 Replies Last reply
      2
      • CharbyC Charby

        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 Offline
        I Offline
        Ibrahim
        wrote on last edited by
        #3

        @Charby Thanks. The problem solved.

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

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

          1 Reply Last reply
          1
          • CharbyC Charby

            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
            
            zeFreelanceZ Offline
            zeFreelanceZ Offline
            zeFreelance
            wrote on last edited by
            #5

            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
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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
              0
              • fcarneyF Offline
                fcarneyF Offline
                fcarney
                wrote on last edited by
                #7

                @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
                0

                • Login

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