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:

  1. you make myclass qobject , create explicit array qscriptengine::newarray, call qscriptengine::newobject every object in array, , add script values array; vectors have

  2. you can register vector type script engine

    qscriptregistersequencemetatype<qvector<myclass> >(engine) 
  3. 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

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -