[Interest] sharing singleton instance between C++ and QML

Stefan Seefeld stefan at seefeld.name
Thu Oct 26 14:41:03 CEST 2023


On Thu, Oct 26, 2023 at 2:53 AM Ulf Hermann via Interest <
interest at qt-project.org> wrote:

> The very common misconception I keep reading over and over is that C++
> singletons should be the same as QML singletons. They generally aren't.
> You can shoehorn a C++ singleton into a QML one, but if you can avoid
> that exercise, you should avoid it.
>

You seem to be thinking that people who don't follow the idioms proposed by
Qt generally just don't know what they are doing, which to me sounds like
Qt is (largely) overestimating its role in a given software architecture.
In some cases (and I wouldn't dare to estimate how frequent they are), Qt
is just one part in a larger system, and therefore has to fit in with
everyone else, rather than trying to impose its own rule.


Now, as always, you _can_ shoot your foot if you really want to. Here is
> how you find the gun: If your singleton does not have a default
> constructor the engine can use, but the engine instead finds a static
> method "Foo *create(QJSEngine *, QQmlEngine *)", it will use that to
> obtain the singleton. The method can also be defined on the QML_FOREIGN
> wrapper in case you cannot touch the actual class at all. In that method
> you can go to town with your singleton. You can create it on the fly or
> retrieve it from somewhere. You can declare its object ownership. You
> can decide yourself which object to return for which engine. You should
> probably check if the thread affinity matches, but if you don't, it's
> your problem.
>

Please stop patronizing people ! Instead, provide a clear definition of
functions such as the above `create()`, including clear rules for how they
are to be used (including object ownership and thread affinity).


Thank you,

-- 

      ...ich hab' noch einen Koffer in Berlin...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20231026/38930caf/attachment.htm>


More information about the Interest mailing list