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:
- a signal (
q_signal
), or - a slot (
q_slot
), or - 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
Post a Comment