When to register c++ singleton types in QML module plugins?
-
Hello.
I have a qml plugin created with CMake/qt_add_qml_module() that contains some C++ QML singletons. I can see the generated x_qmltyperegistrations.cpp file contains normal type registrations for these singletons as well. The module also contains some other non-singleton C++ types which are also registered in that generated cpp file.
These singletons need a call to qmlRegisterSingletonType<>() in order to register their creation functions, but I'm having some issues determining when or where these calls should be made.
If I add them before engine creation, I get some errors on Android builds saying that the module cannot be loaded because the namespace already contains types. Strangely I don't get these errors on the Linux desktop.
I tried to create my own plugin sources and use initializeEngine() to add these functions, but at that point the module has been locked and new types cannot be added.
Any suggestions for how to solve this?
Currently I'm using a hackish solution where I mark the generated x_qmltyperegistrations.cpp file as a HEADER_ONLY and create my own where I include this cpp file and do some preprocessor magic to inject my qmlRegisterSingletonType<>() calls when the types are registered, but this is not pretty.