Unable to access Q_PROPERTY for QML_SINGLETON object.
-
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 aBackEnd { 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
-
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 aBackEnd { 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
@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
-
Thank you very much. It works fine now.