Friday, 15 April 2011

qt - Calling C++ method from QML -



qt - Calling C++ method from QML -

here qt project minimal skeleton show problem (check console after run project) http://uloz.to/xqxrxpdl/qtproject-zip

i seek phone call public slot qml

component.oncompleted: print(model.activate())

still getting error:

typeerror: property 'activate' of object qqmldmobjectdata(0x7fa35dd89eb0) not function

if tried phone call method dynamically c++, works:

auto item = new treeitem<mainmenuitem>(new mainmenuitem("kyklop")); qmetaobject::invokemethod(item, "activate");

if seek access regular property of treeitemtemplatebackend class qml (for instance level), works, problem if calling method. thinking subclass/template class.

registering qml:

qmlregistertype<treeitemtemplatebackend>("engine.ui", 1, 0, "treeitemtemplatebackend"); qmlregistertype<treeitem<inspectoritem>>("engine.ui", 1, 0, "inspectortreeitem"); qmlregistertype<treeitem<mainmenuitem>>("engine.ui", 1, 0, "mainmenutreeitem");

treeitem.h

#ifndef treeitem_h #define treeitem_h #include <qobject> #include "treeitemtemplatebackend.h" template <typename t> class treeitem : public treeitemtemplatebackend { public: explicit treeitem(qobject * parent = null); explicit treeitem(t * data, qobject * parent = null); explicit treeitem(treeitem<t> & other); void addchild(treeitem<t> * child); ~treeitem() {} }; #endif // treeitem_h

treeitemtemplatebackend.h

#ifndef treeitemtemplatebackend_h #define treeitemtemplatebackend_h #include <qlist> #include <qqmllistproperty> class treeitemtemplatebackend : public qobject { q_object q_property(qobject * info read info write setdata notify datachanged) q_property(qqmllistproperty<treeitemtemplatebackend> childs read childs notify childschanged) q_property(int level read level write setlevel notify levelchanged) public: explicit treeitemtemplatebackend(qobject * parent = null); qobject * data() const; void setdata(qobject * data); qqmllistproperty<treeitemtemplatebackend> childs() const; void addchild(treeitemtemplatebackend * child); int level() const; void setlevel(int level); void dump(qstring propertyname) const; ~treeitemtemplatebackend() {} signals: void activated(); void datachanged(); void childschanged(); void levelchanged(); public slots: void activate(); // trying phone call protected: qobject * m_data; qlist<treeitemtemplatebackend *> m_children; int m_level; static void append_function(qqmllistproperty<treeitemtemplatebackend> * property, treeitemtemplatebackend * item); static treeitemtemplatebackend * at_function(qqmllistproperty<treeitemtemplatebackend> * property, int index); static void clear_function(qqmllistproperty<treeitemtemplatebackend> * property); static int count_function(qqmllistproperty<treeitemtemplatebackend> * property); }; #endif // treeitemtemplatebackend_h

qqmldmobjectdata wrapper actual object used in delegates. original object accessible via property modeldata, model.modeldata.activate() should work.

qt qml qtquick2

No comments:

Post a Comment