LimeReport icon indicating copy to clipboard operation
LimeReport copied to clipboard

Own functions in 1.5.x

Open AndrzejWoronko opened this issue 6 years ago • 2 comments

Hi fralx, How to add my own functions in version 1.5.x ? For example I have a function

QScriptValue currentDate(QScriptContext* pcontext, QScriptEngine* pengine);

QScriptValue currentDate(QScriptContext* pcontext, QScriptEngine* pengine) { Q_UNUSED(pcontext) QString str = QDate::currentDate().toString(DATE_FORMAT); return pengine->newVariant(str); }

and in version 1.4.x I added to report in this way:

this->scriptManager()->addFunction("currentDate", currentDate, tr("DATE&TIME"), "currentDate()");

AndrzejWoronko avatar Oct 15 '19 11:10 AndrzejWoronko

There are two-way exist to solve this problem: The first one just compile your project with CONFIG+=qtscriptengine option and addFunction will be accessible. But the QtScript is deprecated and who knows when it will be removed from Qt. The second way is just creating the inheritor of QObject with Q_INVOKABLE functions and register it in the script engine.

class MyFunctionManager: public QObject{
    Q_OBJECT
public:
    Q_INVOKABLE int add(int a, int b){ return a+b;}
};
.......
report->scriptManager()->moveQObjectToScript(new MyFunctionManager(), "MyFunctions");
.......
$S{MyFunctions.add(10,2)}

fralx avatar Oct 15 '19 11:10 fralx

Hi fralx, Thanks a lot, I also added report->scriptManager()->addFunction("add", QString("function add(a,b){return MyFunctions.add(a,b);}"), tr("NUMBER"), "add(a,b)"); and it worked correctly

AndrzejWoronko avatar Oct 16 '19 09:10 AndrzejWoronko