首页 > 其他分享 >QJSEngine

QJSEngine

时间:2023-07-11 18:56:45浏览次数:44  
标签:const QJSEngine myEngine QJSValue scriptFile QString

QJSEngine 

#include <QJSEngine>

 

Public Types

enum Extension { TranslationExtension, ConsoleExtension, GarbageCollectionExtension, AllExtensions }
flags Extensions

Public Functions

  QJSEngine()
  QJSEngine(QObject *parent)
virtual ~QJSEngine() override
void collectGarbage()
QJSValue evaluate(const QString &program, const QString &fileName = QString(), int lineNumber = 1)
T fromScriptValue(const QJSValue &value)
QJSValue globalObject() const
void installExtensions(QJSEngine::Extensions extensions, const QJSValue &object = QJSValue())
QJSValue newArray(uint length = 0)
QJSValue newObject()
QJSValue newQMetaObject(const QMetaObject *metaObject)
QJSValue newQMetaObject()
QJSValue newQObject(QObject *object)
QJSValue toScriptValue(const T &value)

 

 

 

详细说明

QJSEngine类提供了一个用于评估JavaScript代码的环境。

 

案例1:

 QJSEngine myEngine;
 QJSValue three = myEngine.evaluate("1 + 2");

 

案例2:

  QJSValue fun = myEngine.evaluate("(function(a, b) { return a + b; })");
  QJSValueList args;
  args << 1 << 2;
  QJSValue threeAgain = fun.call(args);

 

案例3:

  QString fileName = "helloworld.qs";
  QFile scriptFile(fileName);
  if (!scriptFile.open(QIODevice::ReadOnly))
      // handle error
  QTextStream stream(&scriptFile);
  QString contents = stream.readAll();
  scriptFile.close();
  myEngine.evaluate(contents, fileName);

 

 

 

 

##############################

标签:const,QJSEngine,myEngine,QJSValue,scriptFile,QString
From: https://www.cnblogs.com/herd/p/17498316.html

相关文章