Qml Singleton Wrapper for Q_Gadget Base-Type with static Q_INVOKABLE functions
-
wrote on 2 Mar 2022, 14:37 last edited by
Hey guys,
is there a way to do make a Qml Singleton Wrapper for a Q_GADGET class (not inherinting from QObject) and be able to call static Q_INVOKABLE marked functions (from Base Class) from Qml?
Goal is to not bloat the c++ api with all that QObject inherited functions but have same naming and all between qml/c++. That means the base class cant inherint from QObject - with QObject inherintacne in the base class it would probalby work by exposing the base class directly to qml.Basic Idea is:
class BasicConversions { Q_GADGET public: Q_INVOKABLE static double DegreesToMil(int degrees); Q_INVOKABLE static double DegreesToMil(double degrees); }; namespace MathQml { class BasicConversionsQml : public QObject, public BasicConversions { Q_OBJECT public: static QObject* qmlInstance(QQmlEngine* engine, QJSEngine* scriptEngine); }; } qmlRegisterSingletonType<MathQml::BasicConversionsQml>("Math", 1, 0, "BasicConversions", &MathQml::BasicConversionsQml::qmlInstance);
-
wrote on 14 Mar 2022, 10:38 last edited by
Hi,
I'm afraid exposing the Q_GADGET as a singleton directly just isn't an option.
You may either expose it as an Q_PROPERTY of a QObject or write a wrapper QObject class that has the same Q_INVOKABLEs and then calls the version of the Q_GADGET class internally (this will obviously require adjustments if you decide to change or extend your Q_GADGET class itself).
Hope one of these alternatives is viable for you.