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

Constantin Makshin cmakshin at gmail.com
Sat Oct 28 15:55:33 CEST 2023


Hi Ulf.

MOC is fine, it's the Q_GADGET macro:
#define Q_GADGET_EXPORT(...) \
public: \
     static __VA_ARGS__ const QMetaObject staticMetaObject; \
     void qt_check_for_QGADGET_macro(); \
     typedef void QtGadgetHelper; \
private: \
     QT_WARNING_PUSH \
     Q_OBJECT_NO_ATTRIBUTES_WARNING \
     Q_DECL_HIDDEN_STATIC_METACALL static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); \
     QT_WARNING_POP \
     QT_ANNOTATE_CLASS(qt_qgadget, "") \
     /*end*/

/* qmake ignore Q_GADGET */
#define Q_GADGET Q_GADGET_EXPORT()

On 10/28/23 00:05, Stefan Seefeld wrote:
> Hi Uli,
> 
> thank you for sharing that document, that's indeed very helpful.
> Following its examples, I was able to set up my singleton such that both runtimes (C++, QML) would share the same instance.
> Doing that I bumped into what I would consider a bug, probably in the MOC infrastructure:
> The example I used was
> 
> ```
> struct SingletonForeign
> {
>    Q_GADGET
>    QML_FOREIGN(MySingleton)
>    QML_SINGLETON
>    QML_NAMED_ELEMENT(MySingleton)
> public:
>    static MySingleton *create(QQmlEngine *, QJSEngine *engine);
> };
> 
> ```
> 
> which works fine. Then I wanted to clean the code up a little more, and removed the "public:", given that the above is a `struct`, so everything in it is public anyhow. And that's where it stopped working !
> I assume this is because the MOC isn't really aware of C++ semantics, and for some reason really requires the "public" specifier to be there. Why isn't the MOC using an actual compiler frontend to analyse these declarations ?
> 
> Thanks,
> 
> -- 
> 
>        ...ich hab' noch einen Koffer in Berlin...
> 
> 
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest


More information about the Interest mailing list