qt - Passing a QObject to an Script function with QJSEngine? -
i'm trying call function in external script while passing qobject parameter.
my qobject defined this:
#ifndef insertvalues_h #define insertvalues_h #include <qobject> struct insertvaluedef { qstring name; qstring xmlcode; qstring value; bool key; bool insert; }; typedef insertvaluedef tinsertvaluedef; class insertvalues : public qobject { q_object public: explicit insertvalues(qobject *parent = 0); ~insertvalues(); void insertvalue(tinsertvaluedef value); int count(); void setitemname(int index, qstring name); void setitemxmlcode(int index, qstring xmlcode); void setitemvalue(int index, qstring value); void setitemiskey(int index, bool iskey); void setitemtoinsert(int index, bool toinsert); qstring itemname(int index); qstring itemxmlcode(int index); qstring itemvalue(int index); bool itemiskey(int index); bool itemtoinsert(int index); bool valueisnumber(int index); int getindexbycolumnname(qstring name); private: qlist<tinsertvaluedef> m_insertlist; }; #endif // insertvalues_h
my js script function this:
function beforeinsert(table,data) { if (table == "tmptable") { var index = data.getindexbycolumnname("tmpfield"); if (index >= 0) { data.setitemvalue(index,"carlos quiros"); } } }
the code runs runs script following:
qfile scriptfile(javascript); if (!scriptfile.open(qiodevice::readonly)) { log("error: script file defined cannot opened"); return 1; } jsengine.evaluate(scriptfile.readall(), javascript); scriptfile.close(); insertvalues insertobject; tinsertvaluedef tfield; tfield.key = false; tfield.name = "tmpfield"; tfield.xmlcode = "tmpcode"; tfield.value = "tmpvalue"; tfield.insert = true; insertobject.insertvalue(tfield); qstring error; beforeinsertfunction = jsengine.evaluate("beforeinsert",error); if (!beforeinsertfunction.iserror()) { qjsvalue insertlistobj = jsengine.newqobject(&insertobject); qjsvalue result = beforeinsertfunction.call(qjsvaluelist() << "tmptable" << insertlistobj); if (result.iserror()) { log("error calling beforinsert js function."); return 1; } else { log("js function seems ok"); (int pos = 0; pos < insertobject.count(); pos++) { log(insertobject.itemname(pos) + "-" + insertobject.itemvalue(pos)); } return 1; } } else { log("error evaluating beforinsert js function. [" + error + "]"); return 1; }
i can see parameter "table" passing rest of code not working. guess cannot do:
var index = data.getindexbycolumnname("tmpfield");
any idea doing wrong? , else should make work?
thanks,
in order access properties or invoke methods of qobjects
passed qjsengine
(or qml), need declare them using q_property
, q_invokable
macros in qobject
-derived class declaration.
please see qt documentation more details: exposing attributes of c++ types qml
Comments
Post a Comment