-
I'm trying to register a singleton using the method described here, but it doesn't seem it is being registered properly, or I'm not doing something right. In my The class itself is in a file called class App: public QObject
{
Q_OBJECT
QML_ELEMENT
QML_SINGLETON
...
public:
inline static auto foo = 1;
}; In my Component.onCompleted: {
console.log(App.foo);
} The error I get is
Fair enough. Does anyone know how I can import the singleton? My current work around is to inject it into the root context in my main.cpp: App smvApp;
engine.rootContext()->setContextProperty("App", &smvApp); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Ok my mistake, I should have read this page more carefully. The correct way to register singletons in QT5 is to use the The other method which makes use of |
Beta Was this translation helpful? Give feedback.
-
If anyone has an answer for Qt6, please feel free to share, for others who may have the same question. |
Beta Was this translation helpful? Give feedback.
Ok my mistake, I should have read this page more carefully.
qt_add_qml_module
is only available from QT6.2, while I'm on Qt5.The correct way to register singletons in QT5 is to use the
qmlRegisterSingleInstance
function, as shown here.The other method which makes use of
QML_ELEMENT
, andQML_SINGLETON
, are only available from Qt6