Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Call qmlRegisterType implicitly


  • Qt Champions 2016

    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.


  • Moderators

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


  • Qt Champions 2016

    @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


  • Moderators

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


Log in to reply