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. Unable to access Q_PROPERTY for QML_SINGLETON object.
Forum Updated to NodeBB v4.3 + New Features

Unable to access Q_PROPERTY for QML_SINGLETON object.

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

    I have a weird issue where I cannot seem to be able to access any property declared using Q_PROPERTY when using singleton objects.

    My code is as follows:
    backend.h

    #ifndef BACKEND_H
    #define BACKEND_H
    
    #include <qqml.h>
    
    #include <QJSEngine>
    #include <QObject>
    
    class BackEnd : public QObject {
        Q_OBJECT
        QML_ELEMENT
        QML_SINGLETON
    
        Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
    
       public:
        explicit BackEnd(QObject* = nullptr);
        QString title();
        void setTitle(const QString& title);
    
       signals:
        void titleChanged();
    
       private:
        QString m_title;
    };
    
    #endif  // BACKEND_H
    

    backend.cpp

    #include <backend.h>
    #include <iostream>
    
    QString BackEnd::title() {
        std::cout << "Getter";
        return m_title;
    }
    
    void BackEnd::setTitle(const QString& title) {
        std::cout << "Setter";
        m_title = title;
    
        emit titleChanged();
    }
    
    BackEnd::BackEnd(QObject*) { m_title = "Initial"; }
    

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    int main(int argc, char *argv[]) {
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(
            &engine, &QQmlApplicationEngine::objectCreated, &app,
            [url](QObject *obj, const QUrl &objUrl) {
                if (!obj && url == objUrl) QCoreApplication::exit(-1);
            },
            Qt::QueuedConnection);
        engine.load(url);
    
        return app.exec();
    }
    

    main.qml

    import QtQuick
    import QtQuick.Window
    import self.backend 1.0 as Backend
    
    Window {
        width: 1600
        height: 800
        visible: true
        title: Backend.title
    }
    

    .pro

    QT += quick
    
    CONFIG += qmltypes
    
    HEADERS += \
            backend.h
    
    SOURCES += \
            main.cpp \
            backend.cpp
    
    QML_IMPORT_NAME = self.backend
    QML_IMPORT_MAJOR_VERSION = 1
    
    resources.files = main.qml 
    resources.prefix = /
    RESOURCES += resources
    

    When I compile and run the above code, I get the following:

    qrc:/main.qml:9:5: Unable to assign [undefined] to QString
    

    If I simply comment out QML_SINGLETON and create a BackEnd { id: backend } in QML, backend.title resolves just fine. But with singleton I am unable to access the property.

    Any help would be appreciated.

    Thanks

    raven-worxR 1 Reply Last reply
    0
    • M mossglen90

      I have a weird issue where I cannot seem to be able to access any property declared using Q_PROPERTY when using singleton objects.

      My code is as follows:
      backend.h

      #ifndef BACKEND_H
      #define BACKEND_H
      
      #include <qqml.h>
      
      #include <QJSEngine>
      #include <QObject>
      
      class BackEnd : public QObject {
          Q_OBJECT
          QML_ELEMENT
          QML_SINGLETON
      
          Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
      
         public:
          explicit BackEnd(QObject* = nullptr);
          QString title();
          void setTitle(const QString& title);
      
         signals:
          void titleChanged();
      
         private:
          QString m_title;
      };
      
      #endif  // BACKEND_H
      

      backend.cpp

      #include <backend.h>
      #include <iostream>
      
      QString BackEnd::title() {
          std::cout << "Getter";
          return m_title;
      }
      
      void BackEnd::setTitle(const QString& title) {
          std::cout << "Setter";
          m_title = title;
      
          emit titleChanged();
      }
      
      BackEnd::BackEnd(QObject*) { m_title = "Initial"; }
      

      main.cpp

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      
      int main(int argc, char *argv[]) {
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
          const QUrl url(QStringLiteral("qrc:/main.qml"));
          QObject::connect(
              &engine, &QQmlApplicationEngine::objectCreated, &app,
              [url](QObject *obj, const QUrl &objUrl) {
                  if (!obj && url == objUrl) QCoreApplication::exit(-1);
              },
              Qt::QueuedConnection);
          engine.load(url);
      
          return app.exec();
      }
      

      main.qml

      import QtQuick
      import QtQuick.Window
      import self.backend 1.0 as Backend
      
      Window {
          width: 1600
          height: 800
          visible: true
          title: Backend.title
      }
      

      .pro

      QT += quick
      
      CONFIG += qmltypes
      
      HEADERS += \
              backend.h
      
      SOURCES += \
              main.cpp \
              backend.cpp
      
      QML_IMPORT_NAME = self.backend
      QML_IMPORT_MAJOR_VERSION = 1
      
      resources.files = main.qml 
      resources.prefix = /
      RESOURCES += resources
      

      When I compile and run the above code, I get the following:

      qrc:/main.qml:9:5: Unable to assign [undefined] to QString
      

      If I simply comment out QML_SINGLETON and create a BackEnd { id: backend } in QML, backend.title resolves just fine. But with singleton I am unable to access the property.

      Any help would be appreciated.

      Thanks

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @mossglen90 said in Unable to access Q_PROPERTY for QML_SINGLETON object.:

      title: Backend.title

      should be

      title: Backend.BackEnd.title
      

      and when you dont import it into a separate namespace it would be

      title: BackEnd.title
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mossglen90
        wrote on last edited by
        #3

        Thank you very much. It works fine now.

        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