Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Custom Plugin Class
Forum Updated to NodeBB v4.3 + New Features

Custom Plugin Class

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 429 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    M4cM4rco
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      afalsa
      wrote on last edited by
      #2

      @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
      M 1 Reply Last reply
      1
      • A afalsa

        @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
        M Offline
        M Offline
        M4cM4rco
        wrote on last edited by
        #3

        @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!

        A 1 Reply Last reply
        1
        • M M4cM4rco has marked this topic as solved on
        • M M4cM4rco

          @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!

          A Offline
          A Offline
          afalsa
          wrote on last edited by
          #4

          @M4cM4rco

          Glad to hear it!

          QML macros are fantastic in this case. Just to add more extra info, you can, in fact add more macros to your class depending on your needs

          QML_SINGLETON

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved