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. Qml Singleton Wrapper for Q_Gadget Base-Type with static Q_INVOKABLE functions
Forum Updated to NodeBB v4.3 + New Features

Qml Singleton Wrapper for Q_Gadget Base-Type with static Q_INVOKABLE functions

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 459 Views
  • 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
    Konstantin Schraubek
    wrote on last edited by
    #1

    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);
    
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      Maximilian Goldstein
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved