Call Qt + JS

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.