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. Instanting Q_GADGET
Qt 6.11 is out! See what's new in the release blog

Instanting Q_GADGET

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qtquickc++
3 Posts 3 Posters 3.1k Views 2 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #1

    Hi everyone, http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#value-types describes that Q_GADGET objects are available in QML but there is no example of how to use them in QML (there's only a C++ snippet).
    Could you tell me how to create an instance of the Actor class defined in the documentation in QML/Javascript?
    Thanks in advance

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    sierdzioS 1 Reply Last reply
    0
    • VRoninV VRonin

      Hi everyone, http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#value-types describes that Q_GADGET objects are available in QML but there is no example of how to use them in QML (there's only a C++ snippet).
      Could you tell me how to create an instance of the Actor class defined in the documentation in QML/Javascript?
      Thanks in advance

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      4 years later... but I have stumbled upon this topic and I have an answer, so maybe it will help somebody.

      @VRonin said in Instanting Q_GADGET:

      Could you tell me how to create an instance of the Actor class defined in the documentation in QML/Javascript?

      You cannot instantiate Q_GADGETs in QML. But you can pass them from C++ into QML in 2 ways:

      • via a property of another Q_OBJECT (this way you can even pass a default-constructed gadget, which is practically 100% the same as if you instantiated it in QML)
      • via signal

      For example:

      class Gadget 
      {
        Q_GADGET
        Q_PROPERTY(QString data MEMBER data)
      
      public:
        QString data;
      }
      
      Q_DECLARE_METATYPE(Gadget)
      
      class Object 
      {
        Q_OBJECT
        Q_PROPERTY(Gadget gadget READ gadget)
      
      public:
        Gadget gadget() { 
          Gadget result;
          return result;
        }
      }
      
      // QML:
      Object {
        id: myObject
      
        Component.onCompleted: console.log("Gadget data is:", gadget.data)
      }
      

      (Z(:^

      1 Reply Last reply
      5
      • D Offline
        D Offline
        devel
        wrote last edited by
        #3

        C++:

        class MyValueType
        {
            Q_GADGET
            QML_VALUE_TYPE(myValueType)
            QML_CONSTRUCTIBLE_VALUE
        public:
            Q_INVOKABLE MyValueType(double d = 0.0);
        
            // ...
        };
        

        QML:

        import MyModule as MM
        
        QtObject {
            function process(d: real) {
                let v = new MM.myValueType(d);
                // v is a myValueType now
            }
        }
        
        1 Reply Last reply
        2

        • Login

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