qt - expose C++ class to QML -


i have code.

class pet {   public:    pet(const qstring& nm)     : name(nm)    {}     const qstring& name() const { return nm; }     private:     qstring nm; }  class dog : public qobject, public pet {     q_object   public:     dog(qobject* prnt)      : qbject(prnt),        pet("tommy")     {} } 

exposing qml

qqmlapplicationengine engine; engine.rootcontext()->setproperty("petdog", new dog(&engine)); 

// qml item

console.log(petdog.name()) // typeerror: property 'name' of object dog(0x0xxxxx) not function 

what solution expose methods of c++ class qml? thanks

the methods must known meta-object system in order invokable qml. means method must either:

  1. a signal (q_signal), or
  2. a slot (q_slot), or
  3. invokable (q_invokable).

in qt 5 difference between slot , invokable method of whether method listed among slots when iterate metaobject data. other that, slots , invokable methods equivalent.

in qt 5 can connect c++ method, if it's not slot/invokable, such methods known c++ compiler, not qml.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -