Using the QML_ELEMENT macro properly to avoid error "use of undeclared identifier"
-
Hello, I suppose it might be best practice to use QML_ELEMENT macro instead of manually registering our C++ classes in QML with qmlRegisterType based on this blog post from Qt
https://www.qt.io/blog/qml-type-registration-in-qt-5.15
Our current set-up is this
Main.cpp file has registration of our class
#include "auth/login_manager.hpp" qmlRegisterType<auth::LoginManager>( "dancesyllabuses_loginManager", 1, 0, "LoginManager");
Our class is like this
namespace auth { class LoginManager : public QObject { Q_OBJECT }; } // namespace auth
Now let's say, we want to add our macro
We would change our class like so
#include <QtQml> namespace auth { class LoginManager : public QObject { Q_OBJECT QML_ELEMENT }; } // namespace auth
Then in theory, we no longer need qmlRegisterType in our main.cpp file
And I'm assuming we also no longer need to include the header file for it too in our main.cpp file too. But let me know if I'm wrong about that.
Regardless, however, here's where I run into a problem
In the auto generated file, the program complains
appdance_syllabuses_lib_qmltyperegistrations.cpp:23:34: error: use of undeclared identifier 'auth' qmlRegisterTypesAndRevisions<auth::LoginManager>("Dance_Syllabuses", 1); ^
Perhaps the problem is a result of using namespaces like Qt doesn't like that
Perhaps the problem is something I've missed with transitioning to using our macro like I might have had to do more things within our class or to cmakelists.txt or something. Let me know if it's programmer mistake!
Any help with getting past this issue would be greatly appreciated! =)
-
If you are using Cmake then you have to tell where to find files that implement classes using QML_ELEMENT macro
# Add include directories so target can find the QMLELEMENTS set(QML_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/path/to/src ) # Add includepaths target_include_directories(target PRIVATE ${QML_INCLUDES} )
-
K Kory has marked this topic as solved
-
K Kory has marked this topic as unsolved
-
Yeah that's all working now
I was receiving some errors
W/default : qrc:/Main.qml:10:1: static plugin for module "URI " with name "URIPlugin" cannot be loaded: Namespace 'URI' has already been used for type registration.
GlobalContextModule/pages/SignUp.qml:204 RolesList is not a type
Solution to this I believe was to
- Make sure I hadn't repeated the import statement for our URI from the Parent cmakelists.txt. Where URI refers to URI of our target that we provided to target_include_directories. And our URI is declared in qt_add_qml_module
- Move the RolesList{} QML element from page in child module to Main.qml where our import statement was. Even though Main.qml loads this page in and in effect, should provide it with what it wants, it didn't for me in practice. Before we used QML_ELEMENT I had an import statement just for RolesList{} in page of child module but now that thing is in our Import URI in Main.qml. If that doesn't make any sense, basically all I'm saying is don't assume classes put inside of main module via QML_ELEMENT and CmakeLIsts.txt get passed down to child modules included into this main module
-
K Kory has marked this topic as solved