Custom Plugin Class
-
Hello,
I'm working on developing a plugin that includes both QML and C++ classes. Everything seems to work fine so far. However, I've run into an issue with registering an enum from one of the C++ classes. After a long search, I found a workaround - though I suspect it's not the correct approach - by registering the type directly in the constructor of the automatically generated xplugin_xPlugin.cpp file in the build directory. Given that this file is marked with a comment advising against manual edits due to it being auto-generated by CMake, I decided to attempt writing my own plugin class.
In my CMake configuration, for qt_add_qml_module, I added:
NO_GENERATE_PLUGIN_SOURCE NO_PLUGIN_OPTIONAL
and changed the linking from Tableplugin to Table with:
target_link_libraries(ExampleProject PRIVATE Qt6::Quick Table)
I created a class and did a copy-paste from xplugin_xPlugin.cpp, including adding the global function qml_register_types_x(). However, upon running the program, I encountered a linking error pointing to
Line Q_IMPORT_QML_PLUGIN(x): undefined reference to 'qt_static_plugin_x()'.
Not knowing what qt_static_plugin_x() does, I simply added an empty function in my custom plugin class. This resolved the linking error, but then I received an access violation error:
Process finished with exit code -1073741819 (0xC0000005)
From this, I deduce that I've made several mistakes, especially regarding qt_static_plugin_x(), but I'm struggling to find helpful information online. I'm hoping you might be able to assist me. I would greatly appreciate a detailed response, and if you need any more information from me, please let me know.
Many thanks,
Marco -
@M4cM4rco said in Custom Plugin Class:
Hello Marco!
Some questions:
-
How did you registered the enum in your class? Is a QObject class? Did you add Q_ENUM macro to that enum?
-
Did you add QML_ELEMENT or QML_NAMED_ELEMENT() to that class? (In this case, if you add any macro there is no need to add a global function to register any type )
-
Why did you added
NO_GENERATE_PLUGIN_SOURCE
? As seen in docs:
Note: When using the CMake qt_add_qml_module API, a plugin will be generated automatically for you. It will take care of type registration. You only need to write a custom plugin if you have special requirements, such as registering custom image providers. In that case, pass NO_GENERATE_PLUGIN_SOURCE to the qt_add_qml_module call to disable the generation of the default plugin.
- Did you follow this? Creating C++ Plugins for QML
-
-
@M4cM4rco said in Custom Plugin Class:
Hello Marco!
Some questions:
-
How did you registered the enum in your class? Is a QObject class? Did you add Q_ENUM macro to that enum?
-
Did you add QML_ELEMENT or QML_NAMED_ELEMENT() to that class? (In this case, if you add any macro there is no need to add a global function to register any type )
-
Why did you added
NO_GENERATE_PLUGIN_SOURCE
? As seen in docs:
Note: When using the CMake qt_add_qml_module API, a plugin will be generated automatically for you. It will take care of type registration. You only need to write a custom plugin if you have special requirements, such as registering custom image providers. In that case, pass NO_GENERATE_PLUGIN_SOURCE to the qt_add_qml_module call to disable the generation of the default plugin.
- Did you follow this? Creating C++ Plugins for QML
@afalsa, thanks for your insightful questions!
I'm working with a QObject class and utilizing the enum macro. Here's a quick look at my Test class:
#include <QObject> #include <qDebug> class Test : public QObject { Q_OBJECT public: Test(QObject *parent = nullptr) { qDebug() << "Test created"; } enum TestEnum { First, Second, Third, }; Q_ENUM(TestEnum) };
Your suggestion to add QML_ELEMENT was an excellent tip that hadn't crossed my mind. With that, I don't need to manually register the type, and I can directly reference it in QML as Test.Third. This approach really simplifies things and perfectly suits my needs.
I initially used NO_GENERATE_PLUGIN_SOURCE thinking it was necessary for manual enum type registration, but it turns out that's not required with your advice.
Thanks a ton for your help, afalsa! Have a fantastic day!
-
-
-
@afalsa, thanks for your insightful questions!
I'm working with a QObject class and utilizing the enum macro. Here's a quick look at my Test class:
#include <QObject> #include <qDebug> class Test : public QObject { Q_OBJECT public: Test(QObject *parent = nullptr) { qDebug() << "Test created"; } enum TestEnum { First, Second, Third, }; Q_ENUM(TestEnum) };
Your suggestion to add QML_ELEMENT was an excellent tip that hadn't crossed my mind. With that, I don't need to manually register the type, and I can directly reference it in QML as Test.Third. This approach really simplifies things and perfectly suits my needs.
I initially used NO_GENERATE_PLUGIN_SOURCE thinking it was necessary for manual enum type registration, but it turns out that's not required with your advice.
Thanks a ton for your help, afalsa! Have a fantastic day!