When using Qt to call a function, use the following method: QScriptEngine engine; engine. evaluate ("function fullName () {return this. firstName + ''+ this. lastName;} "); engine. evaluate ("somePerson = {firstName: 'john', lastName: 'doe '}"); QScriptValue global = engine. globalObject (); QScriptValue fullName = global. property ("fullName"); QScriptValue who = global. property ("somePerson"); qDebug () <fullName. call (who ). toString (); // "John Doe "Engine. evaluate ("function cube (x) {return x * x;}"); QScriptValue cube = global. property ("cube"); QScriptValueList args; args <3; qDebug () <cube. call (QScriptValue (), args ). toNumber (); // 27, however, the script cannot have a statement such as return, which is returned using the last line. This is based on multiple tests and may be related to the compiling environment, in the second version of <C ++ GUI Qt4 programming>, the scripts in the example of the calculator in Chapter 1 cannot be correctly executed and cannot be understood. Finally, you have to switch to the function method, no problem, but the method using the function is too stiff, so I tried to modify it, QFilefile ("test. js"); if (! File. open (QIODevice: ReadOnly) {abortOperation (); return;} QTextStream in (& file); in. setCodec ("UTF-8"); // in. setCodec ("GBK"); QString script = in. readAll (); qDebug () <script; file. close (); qDebug () <display-> text (). toDouble (); QScriptEngine interpreter; QScriptValue operand (& interpreter, display-> text (). toDouble (); interpreter. globalObject (). setProperty ("x", operand); // set the global property x here. You can call qDebu in js. G () <interpreter. globalObject (). property ("x "). toNumber (); // QScriptValue myObject = interpreter. newObject (); // myObject. setProperty ("x", operand); QString fileName = "helloworld. qs "; // Save the error message. QScriptValueList args1; args1 <operand; QScriptValue result = interpreter. evaluate (script, fileName); qDebug () <result. toNumber () <result. toString (); qDebug () <interpreter. globalObject (). property ("x "). toNum Ber (); QScriptValue test = result. call (QScriptValue (), args1); // qDebug () <result; QScriptEngine myEngine; QScriptValue fun = myEngine. evaluate ("(function (a, B) {return a + B;})"); QScriptValueList args; args <1 <2; QScriptValue threeAgain = fun. call (QScriptValue (), args); qDebug () <threeAgain. toNumber (); qDebug () <test. toNumber (); if (! Result. isNumber () {// abortOperation (); return;} the original JavaScript code is function factorial (n) {if (n <= 1) {return 1 ;} else {return n * factorial (n-1) ;}} return factorial (Math. floor (x); change to function factorial (n) {if (n <= 1) {return 1;} else {return n * factorial (n-1 );}} result = factorial (Math. floor (x); OK, no problem my non-Recursive Method result = 1; while (x> 1) result * = x --; x = result; in this way, the program can be correctly executed according to the example.