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. When to register c++ singleton types in QML module plugins?
Forum Updated to NodeBB v4.3 + New Features

When to register c++ singleton types in QML module plugins?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlmodulepluginsingletonc++
3 Posts 3 Posters 772 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.
  • K Offline
    K Offline
    Knutid
    wrote on 26 Oct 2022, 22:44 last edited by
    #1

    Hello.

    I have a qml plugin created with CMake/qt_add_qml_module() that contains some C++ QML singletons. I can see the generated x_qmltyperegistrations.cpp file contains normal type registrations for these singletons as well. The module also contains some other non-singleton C++ types which are also registered in that generated cpp file.

    These singletons need a call to qmlRegisterSingletonType<>() in order to register their creation functions, but I'm having some issues determining when or where these calls should be made.

    If I add them before engine creation, I get some errors on Android builds saying that the module cannot be loaded because the namespace already contains types. Strangely I don't get these errors on the Linux desktop.

    I tried to create my own plugin sources and use initializeEngine() to add these functions, but at that point the module has been locked and new types cannot be added.

    Any suggestions for how to solve this?
    Currently I'm using a hackish solution where I mark the generated x_qmltyperegistrations.cpp file as a HEADER_ONLY and create my own where I include this cpp file and do some preprocessor magic to inject my qmlRegisterSingletonType<>() calls when the types are registered, but this is not pretty.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Dika
      wrote 14 days ago last edited by
      #2

      Hello, did you eventually solve this problem somehow?

      J 1 Reply Last reply 12 days ago
      0
      • D Dika
        14 days ago

        Hello, did you eventually solve this problem somehow?

        J Offline
        J Offline
        JKSH
        Moderators
        wrote 12 days ago last edited by
        #3

        @Dika The solution is to:

        1. Register your class using QML_SINGLETON
        2. Do not call qmlRegisterSingletonType<>()
        3. If you don't have (or don't want) a default constructor, implement a static member function called create() to act as a factory. See https://doc.qt.io/qt-6/qqmlintegration-h-qtqml-proxy.html#QML_SINGLETON for an example

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        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