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: refrence error
Forum Updated to NodeBB v4.3 + New Features

QQmlContext::setContextProperty: refrence error

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 520 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.
  • U Offline
    U Offline
    UbiMiles
    wrote on last edited by UbiMiles
    #1

    Hi , I've searched throughout the forum and StackOverflow and I still can't seem to find a solution for this error or know what is causing it

    //testing.h
    #ifndef TESTING_H
    #define TESTING_H
    
    #include <QObject>
    
    class testing : public QObject
    {
        Q_OBJECT
    public:
        explicit testing(QObject *parent = nullptr);
        QString test="this is working";
    signals:
    
    public slots:
    
    
    };
    
    #endif // TESTING_H
    
    
    //main.cpp
    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlEngine>
    #include <QQmlContext>
    #include "testing.h"
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        testing tst;
        engine.rootContext()->setProperty("tester",QVariant::fromValue(&tst));
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        engine.load(url);
        return app.exec();
    }
    
    //main.qml
    import QtQuick 2.15
    import QtQuick.Window 2.15
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("PlayGround")
        Text {
            id: testText
            text: tester.test
        }
    }
    

    47775d3a-d3f5-4398-a974-1eb3a3fcc204-image.png

    I am new to the QML scene if the issue is me using a QString then apologies, however it doesn't work with any class and doesn't work with the Connections type for some reason
    I tried setting the context property in different places(lines) in the code, and it still doesn't work for some reason
    please help!
    Thanks in advance !

    KroMignonK 1 Reply Last reply
    0
    • U UbiMiles

      Hi , I've searched throughout the forum and StackOverflow and I still can't seem to find a solution for this error or know what is causing it

      //testing.h
      #ifndef TESTING_H
      #define TESTING_H
      
      #include <QObject>
      
      class testing : public QObject
      {
          Q_OBJECT
      public:
          explicit testing(QObject *parent = nullptr);
          QString test="this is working";
      signals:
      
      public slots:
      
      
      };
      
      #endif // TESTING_H
      
      
      //main.cpp
      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <QQmlEngine>
      #include <QQmlContext>
      #include "testing.h"
      
      int main(int argc, char *argv[])
      {
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
          testing tst;
          engine.rootContext()->setProperty("tester",QVariant::fromValue(&tst));
          const QUrl url(QStringLiteral("qrc:/main.qml"));
          engine.load(url);
          return app.exec();
      }
      
      //main.qml
      import QtQuick 2.15
      import QtQuick.Window 2.15
      
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("PlayGround")
          Text {
              id: testText
              text: tester.test
          }
      }
      

      47775d3a-d3f5-4398-a974-1eb3a3fcc204-image.png

      I am new to the QML scene if the issue is me using a QString then apologies, however it doesn't work with any class and doesn't work with the Connections type for some reason
      I tried setting the context property in different places(lines) in the code, and it still doesn't work for some reason
      please help!
      Thanks in advance !

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @UbiMiles said in QQmlContext::setContextProperty: refrence error:

      I tried setting the context property in different places(lines) in the code, and it still doesn't work for some reason
      please help!

      Hello and welcome to Qt Forum.
      You use the false function, you should use setContextProperty():

      engine.rootContext()->setContextProperty("tester", &tst);
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      U 1 Reply Last reply
      3
      • KroMignonK KroMignon

        @UbiMiles said in QQmlContext::setContextProperty: refrence error:

        I tried setting the context property in different places(lines) in the code, and it still doesn't work for some reason
        please help!

        Hello and welcome to Qt Forum.
        You use the false function, you should use setContextProperty():

        engine.rootContext()->setContextProperty("tester", &tst);
        
        U Offline
        U Offline
        UbiMiles
        wrote on last edited by
        #3

        @KroMignon
        I feel stupid yet I'm sincerely thankful
        I've been stuck for a couple of days now and I don't think I would've noticed that on my own
        thanks a lot !

        KroMignonK 1 Reply Last reply
        1
        • U UbiMiles

          @KroMignon
          I feel stupid yet I'm sincerely thankful
          I've been stuck for a couple of days now and I don't think I would've noticed that on my own
          thanks a lot !

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

          @UbiMiles Your welcome... I also had done this kind of mistake days ago, don't be too rude to yourself ;)

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          1

          • Login

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