how to call user defined function of user defined call from QtScriptEngine evaluate
Hi,
I am having a c++ class CRectangleItems. this class have lots of different functions. I want to call these function by using QtScript. Means i will create object of CRectangle and will define as newQObject with QtScriptEngine. I will provide separate script editor, from editor user will call and of function from CRectangleItems . and all that script will be given to QtScriptEngine.evaluate function. Please guide me to achieve this.
Re: how to call user defined function of user defined call from QtScriptEngine evalua
It's enough if your CRectangleItems class declares all methods you want to call on it as slots. Then you can call them from within QtScript (provided the instance of the class is exported to the scripting environment).
Re: how to call user defined function of user defined call from QtScriptEngine evalua
yes i tried to do it in same way, but its not working. have a look towards the code
QScriptEngine scriptEngine;
CRectangleItem *rectangle = new CRectangleItem();
QScriptValue scriptobj = scriptEngine.newQObject(rectangle,QScriptEngine::S criptOwnership);
scriptEngine.globalObject().setProperty("rectangle ",scriptobj);
QScriptValue result = scriptEngine.evaluate(scriptstring);
In CRectangleItem class i have declared a slot drawr()
then can i call it from script editor like
rectangle.drawr(); ----- I will give this string to evaluate function.... is it correct way of calling, please let me know
Re: how to call user defined function of user defined call from QtScriptEngine evalua
Quote:
Originally Posted by
sheetalw
yes i tried to do it in same way, but its not working. have a look towards the code
QScriptEngine scriptEngine;
CRectangleItem *rectangle = new CRectangleItem();
QScriptValue scriptobj = scriptEngine.newQObject(rectangle,QScriptEngine::S criptOwnership);
scriptEngine.globalObject().setProperty("rectangle ",scriptobj);
QScriptValue result = scriptEngine.evaluate(scriptstring);
In CRectangleItem class i have declared a slot drawr()
then can i call it from script editor like
rectangle.drawr();
And what happens when you do that?
Re: how to call user defined function of user defined call from QtScriptEngine evalua
string variable result shows a message " rectangle.drawr[undefined] is not a function"
Re: how to call user defined function of user defined call from QtScriptEngine evalua
This works fine for me:
Code:
#include <QApplication>
#include <QScriptEngine>
#include <QtDebug>
Q_OBJECT
public:
public slots:
void drawr() { qDebug() << Q_FUNC_INFO; }
};
#include "main.moc"
int main(int argc, char **argv) {
QScriptEngine engine;
RItem item;
QScriptValue val = engine.newQObject(&item);
engine.globalObject().setProperty("rectangle", val);
engine.evaluate("rectangle.drawr()") ;
return 0;
}
Re: how to call user defined function of user defined call from QtScriptEngine evalua
Thanks a lot. I declared slot as private that's why it was not working for me.