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. Q_PROPERTY Produces TypeError When Setting QQuickWidget ContextProperty
Forum Updated to NodeBB v4.3 + New Features

Q_PROPERTY Produces TypeError When Setting QQuickWidget ContextProperty

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 2 Posters 154 Views 2 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.
  • D Offline
    D Offline
    daviddev
    wrote last edited by
    #1

    Hello everyone. I am setting a Q_OBJECT class with a QString Q_PROPERTY in a QQuickWidget. The application loads and displays the test text perfectly fine. However it always produces the error

    TypeError: Cannot read property 'message' of null
    

    Debugging shows the type error is produced in the Application Output in MainWindow's constructor where

    ui->qmlQuickWidget->rootContext()->setContextProperty("testQML", &testQML);
    

    and the QML Debugger Console from w.show() in main.

    Is there a way of setting the QML file that will remove this error from occurring or a better implementation in the .qml file itself?

    I am running on QT 6.9.3 on Windows 10. All the relevant code is below. Thank you.

    //TestQML.qml
    import QtQuick 2.15
    
    Text{
        text: testQML.message
    }
    
    
    //MainWindow.cpp
    #include "MainWindow.hpp"
    #include "TestQML.hpp"
    #include "./ui_mainwindow.h"
    #include <QQmlContext>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow){
        ui->setupUi(this);
        TestQML testQML;
        testQML.setMessage(QString("test"));
        ui->qmlQuickWidget->rootContext()->setContextProperty("testQML", &testQML);
        ui->qmlQuickWidget->setSource(QUrl::fromLocalFile("../../TestQML.qml"));
    
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    //TestQML.cpp
    #include "TestQML.hpp"
    
    TestQML::TestQML(QObject *parent)
        : QObject(parent){
        myMessage = "Test Message";
    }
    
    QString TestQML::message() const {
        return myMessage;
    }
    
    void TestQML::setMessage(const QString &newMessage){
        if (myMessage != newMessage) {
            myMessage = newMessage;
            emit messageChanged();
        }
    }
    
    
    //TestQML.hpp
    #ifndef TESTQML_HPP
    #define TESTQML_HPP
    
    #include <QObject>
    #include <QString>
    
    class TestQML : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString message READ message WRITE setMessage NOTIFY messageChanged)
    public:
        explicit TestQML(QObject *parent = nullptr);
        QString  message() const;
        void setMessage(const QString &newMessage);
    signals:
        void messageChanged();
    
    private:
        QString myMessage;
    };
    
    #endif // TESTQML_HPP
    
    
    JKSHJ 1 Reply Last reply
    0
    • D daviddev

      Hello everyone. I am setting a Q_OBJECT class with a QString Q_PROPERTY in a QQuickWidget. The application loads and displays the test text perfectly fine. However it always produces the error

      TypeError: Cannot read property 'message' of null
      

      Debugging shows the type error is produced in the Application Output in MainWindow's constructor where

      ui->qmlQuickWidget->rootContext()->setContextProperty("testQML", &testQML);
      

      and the QML Debugger Console from w.show() in main.

      Is there a way of setting the QML file that will remove this error from occurring or a better implementation in the .qml file itself?

      I am running on QT 6.9.3 on Windows 10. All the relevant code is below. Thank you.

      //TestQML.qml
      import QtQuick 2.15
      
      Text{
          text: testQML.message
      }
      
      
      //MainWindow.cpp
      #include "MainWindow.hpp"
      #include "TestQML.hpp"
      #include "./ui_mainwindow.h"
      #include <QQmlContext>
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow){
          ui->setupUi(this);
          TestQML testQML;
          testQML.setMessage(QString("test"));
          ui->qmlQuickWidget->rootContext()->setContextProperty("testQML", &testQML);
          ui->qmlQuickWidget->setSource(QUrl::fromLocalFile("../../TestQML.qml"));
      
      
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      
      //TestQML.cpp
      #include "TestQML.hpp"
      
      TestQML::TestQML(QObject *parent)
          : QObject(parent){
          myMessage = "Test Message";
      }
      
      QString TestQML::message() const {
          return myMessage;
      }
      
      void TestQML::setMessage(const QString &newMessage){
          if (myMessage != newMessage) {
              myMessage = newMessage;
              emit messageChanged();
          }
      }
      
      
      //TestQML.hpp
      #ifndef TESTQML_HPP
      #define TESTQML_HPP
      
      #include <QObject>
      #include <QString>
      
      class TestQML : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(QString message READ message WRITE setMessage NOTIFY messageChanged)
      public:
          explicit TestQML(QObject *parent = nullptr);
          QString  message() const;
          void setMessage(const QString &newMessage);
      signals:
          void messageChanged();
      
      private:
          QString myMessage;
      };
      
      #endif // TESTQML_HPP
      
      
      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote last edited by
      #2

      @daviddev The main issue is that your TestQML object is a local variable -- it gets destroyed as soon as the MainWindow constructor returns. So, a quick fix is to allocate your TestQML object on the heap (auto testQML = new TestQML(this);).

      However, that is not the recommended fix. We recommend making your TestQML object a QML singleton, using the QML_ELEMENT and QML_SINGLETON macros (see https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html )

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      2
      • D Offline
        D Offline
        daviddev
        wrote last edited by
        #3

        I greatly appreciate this. Would there be a project that has a QML_SINGLETON implementation with a QQuickWidget you know about? The closest source I have discovered is this forum post however it uses a QQMLApplicationEngine connection while I am trying to use QQuickWidget. The closest Example QT provides is the QQuickWidgetVersusWindow_opengl which I am getting lost in.
        https://forum.qt.io/topic/133764/unable-to-access-q_property-for-qml_singleton-object?_=1761145429336

        Also, when should I use the pragma Singleton in the .qml file vs the QML_ELEMENT and QML_SINGLETON?

        JKSHJ 1 Reply Last reply
        0
        • D Offline
          D Offline
          daviddev
          wrote last edited by
          #4

          I have resolved the TypeError error with both a Singleton approach as well as the heap approach. I am trying to post the heap solution below, but it keeps getting marked as spam, as well as the Qml implenetation for the Singleton. I have unfortunately had to use a pointer in the MainWindow header file for the QQuickWidget and set it in the Window's constructor.

          Would somebody know how to setup the Singleton version that will update the QQuickWidget? I am able to setup the Singleton version, however I have not been able to update it for the life of me.

          1 Reply Last reply
          1
          • D daviddev

            I greatly appreciate this. Would there be a project that has a QML_SINGLETON implementation with a QQuickWidget you know about? The closest source I have discovered is this forum post however it uses a QQMLApplicationEngine connection while I am trying to use QQuickWidget. The closest Example QT provides is the QQuickWidgetVersusWindow_opengl which I am getting lost in.
            https://forum.qt.io/topic/133764/unable-to-access-q_property-for-qml_singleton-object?_=1761145429336

            Also, when should I use the pragma Singleton in the .qml file vs the QML_ELEMENT and QML_SINGLETON?

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote last edited by
            #5

            @daviddev said in Q_PROPERTY Produces TypeError When Setting QQuickWidget ContextProperty:

            when should I use the pragma Singleton in the .qml file vs the QML_ELEMENT and QML_SINGLETON?

            • Use the pragma if you implement your singleton in QML
            • Use QML_ELEMENT if you implement your singleton in C++

            Would somebody know how to setup the Singleton version that will update the QQuickWidget? I am able to setup the Singleton version, however I have not been able to update it for the life of me.

            The QML engine instantiates the singleton, so you don't do that manually.

            You can retrieve a pointer to your singleton by calling QQmlEngine::singletonInstance<>(): https://doc.qt.io/qt-6/qqmlengine.html#singletonInstance-1

            // Replace "MyModule" with your actual module's name
            auto instance = ui->qmlQuickWidget->engine()->singletonInstance<TestQML*>("MyModule", "TestQML");
            instance->updateSomething();
            

            Note: To avoid confusion, I recommend that you give your C++ class and your *.qml file different names

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            2
            • D Offline
              D Offline
              daviddev
              wrote last edited by
              #6

              Thank you @JKSH this really helped.

              1 Reply Last reply
              0
              • D daviddev has marked this topic as solved

              • Login

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