c++ - Expose a template-based container class to Qt script engine? -
situation
i have few instances of qvector<myclass>
want them exposed qscriptengine
.
in project, myclass
cache multi-dimensional data "point", , looks like:
class myclass { public: myclass(); /* functions */ private: int m_index; double m_time; qlist<int> *m_data; };
the reason why doing because want users able write ecma script process these "points" comes different sets.
question
how expose whole container , it's contents qt script engine?
i know can make myclass
qobject
, call qscriptengine::newobject
it's qscriptvalue
, set value engine's global object. expose "one point" script engine, , need pass whole instance of qvector<myclass>
.
(any other kind of workaround welocme too!)
one of should work:
you make
myclass
qobject , create explicit arrayqscriptengine::newarray
, callqscriptengine::newobject
every object in array, , add script values array; vectors haveyou can register vector type script engine
qscriptregistersequencemetatype<qvector<myclass> >(engine)
you make own script value type:
q_declare_metatype(qvector<myclass>); qregistermetatype<qvector<myclass> >(); qscriptregistermetatype<qvector<myclass> >(engine, qscriptvaluefromvector, qscriptvaluetovector, qscriptvalue()); //arbitrary conversion functions (you create new object, pointer vector): qscriptvalue qscriptvaluefromvector(qscriptengine *engine, qvector<myclass> const &list) { qscriptvalue result = engine->newarray(list.size()); (int i=0;i<list.size();i++) result.setproperty(i, engine->newqobject(list[i])); return result; } void qscriptvaluetostringptr(const qscriptvalue &value, qvector<myclass> &list) { list.clear(); qscriptvalueiterator it(def); while (it.hasnext()) { it.next(); if (it.flags() & qscriptvalue::skipinenumeration) continue; list << *it.value().toqobject(); } }
Comments
Post a Comment