PDA

View Full Version : Scripting in Qt



Kamalpreet
13th November 2014, 10:09
I have a widgets application. I am trying to implement scripting in this application.

The user will create a script using javascript which will be executed on pressing a QPushButton.

The script is expected to be of the form:


function drawCircle()
{
center(10, 10);
radius(10);
}

I am trying to use QJSEngine class. How the evaluation of script is to be done?

I have tried the following code snippet in the application:



QJSValue function = jsEngine->evaluate("function drawCircle() {center(x, y); radius(r);}");
QJSValueList args;
args << 1;
QJSValue result = function.call(args);
int rslt = result.toInt();
qDebug() << rslt;

Is this the right way to add scripting to the application? Please enlighten me on this.

anda_skoa
13th November 2014, 11:03
In general yes, but you might want to look at QScriptEngine instead.
Theoretically QJSEngine was intended to be its replacement but its non-QML development has been suspended for now.

Cheers,
_

Kamalpreet
13th November 2014, 14:55
In general yes, but you might want to look at QScriptEngine instead.
Theoretically QJSEngine was intended to be its replacement but its non-QML development has been suspended for now.
_

Okay. So I should use QScriptEngine. Can you point me out to some examples having form as I need?

wysota
13th November 2014, 15:23
There is a bunch of examples on QtScript bundled with Qt.