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. Call qmlRegisterType implicitly
Qt 6.11 is out! See what's new in the release blog

Call qmlRegisterType implicitly

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 1.4k 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.
  • benlauB Offline
    benlauB Offline
    benlau
    Qt Champions 2016
    wrote on last edited by benlau
    #1

    Hello,

    I am writing few QML component library. To avoid user to register QML type explicitly before loading QML component, I put those qmlRegisterType calls in the constructor of a static variable. Like this:

    class QFQmlTypes {
    
    public:
        QFQmlTypes() {
            qmlRegisterType<QFAppListener>("QuickFlux", 1, 0, "AppListener");
            qmlRegisterType<QFAppScript>("QuickFlux", 1, 0, "AppScript");
        }
    };
    
    static QFQmlTypes registerHelper;
    

    That works perfectly in Mac/Linux/Android/iOS. However, I got a bug report that it will crash with MSVC. Probably because those code is executed before the construction of QGuiApplication.

    Is there any alternative methods?

    I have tried to write in QQmlExtensionPlugin, but can't figure out how to make it works with static linking.

    raven-worxR 1 Reply Last reply
    0
    • benlauB benlau

      Hello,

      I am writing few QML component library. To avoid user to register QML type explicitly before loading QML component, I put those qmlRegisterType calls in the constructor of a static variable. Like this:

      class QFQmlTypes {
      
      public:
          QFQmlTypes() {
              qmlRegisterType<QFAppListener>("QuickFlux", 1, 0, "AppListener");
              qmlRegisterType<QFAppScript>("QuickFlux", 1, 0, "AppScript");
          }
      };
      
      static QFQmlTypes registerHelper;
      

      That works perfectly in Mac/Linux/Android/iOS. However, I got a bug report that it will crash with MSVC. Probably because those code is executed before the construction of QGuiApplication.

      Is there any alternative methods?

      I have tried to write in QQmlExtensionPlugin, but can't figure out how to make it works with static linking.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @benlau said:

      That works perfectly in Mac/Linux/Android/iOS. However, I got a bug report that it will crash with MSVC. Probably because those code is executed before the construction of QGuiApplication.

      exactly. you can't rely on the static initialization order.

      I have tried to write in QQmlExtensionPlugin, but can't figure out how to make it works with static linking.

      what do you mean "make it work with static linking"? What doesn't work?
      Did you use Q_IMPORT_PLUGIN macro?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      benlauB 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @benlau said:

        That works perfectly in Mac/Linux/Android/iOS. However, I got a bug report that it will crash with MSVC. Probably because those code is executed before the construction of QGuiApplication.

        exactly. you can't rely on the static initialization order.

        I have tried to write in QQmlExtensionPlugin, but can't figure out how to make it works with static linking.

        what do you mean "make it work with static linking"? What doesn't work?
        Did you use Q_IMPORT_PLUGIN macro?

        benlauB Offline
        benlauB Offline
        benlau
        Qt Champions 2016
        wrote on last edited by
        #3

        @raven-worx said:

        what do you mean "make it work with static linking"? What doesn't work?
        Did you use Q_IMPORT_PLUGIN macro?

        Thanks for reply. I used a wrong term. It should be source linking. Although it is a library project, it don't build as an ".a" file. Instead, it is distributed with source code, and user include all the source code in their project.

        Anyway, seem that QQmlExtensionPlugin is not a solution to call qmlRegisterType implicitly. I got another solution that use Q_COREAPP_STARTUP_FUNCTION from :

        https://github.com/benlau/quickflux/issues/7#issuecomment-229201973

        raven-worxR 1 Reply Last reply
        0
        • benlauB benlau

          @raven-worx said:

          what do you mean "make it work with static linking"? What doesn't work?
          Did you use Q_IMPORT_PLUGIN macro?

          Thanks for reply. I used a wrong term. It should be source linking. Although it is a library project, it don't build as an ".a" file. Instead, it is distributed with source code, and user include all the source code in their project.

          Anyway, seem that QQmlExtensionPlugin is not a solution to call qmlRegisterType implicitly. I got another solution that use Q_COREAPP_STARTUP_FUNCTION from :

          https://github.com/benlau/quickflux/issues/7#issuecomment-229201973

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @benlau
          ok great.
          anyway, i think the method would be called when the plugin would be built statically and Q_IMPORT_PLUGIN would be used. Then the QPluginLoader class should load it like a shared lib.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0

          • Login

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