本文整理汇总了C++中QScriptValue::construct方法的典型用法代码示例。如果您正苦于以下问题:C++ QScriptValue::construct方法的具体用法?C++ QScriptValue::construct怎么用?C++ QScriptValue::construct使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QScriptValue
的用法示例。
在下文中一共展示了QScriptValue::construct方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char **argv)
{
Q_INIT_RESOURCE(rtfedit);
QApplication app(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
QScriptEngine engine;
QFile scriptFile(":/rtfedit.js");
scriptFile.open(QIODevice::ReadOnly);
engine.evaluate(QObject::tr(scriptFile.readAll()));
scriptFile.close();
RTFUiLoader loader;
QFile uiFile(":/rtfedit.ui");
uiFile.open(QIODevice::ReadOnly);
QWidget *ui = loader.load(&uiFile);
uiFile.close();
QScriptValue func = engine.evaluate("RTF");
QScriptValue scriptUi = engine.newQObject(ui);
QScriptValue table = func.construct(QScriptValueList() << scriptUi);
if(engine.hasUncaughtException()) {
QScriptValue value = engine.uncaughtException();
QString lineNumber = QString("\nLine Number:%1\n").arg(engine.uncaughtExceptionLineNumber());
QStringList btList = engine.uncaughtExceptionBacktrace();
QString trace;
for(short i=0; i<btList.size(); ++i)
trace += btList.at(i);
QMessageBox::information(NULL, QObject::tr("Exception"), value.toString() + lineNumber + trace );
}
ui->show();
return app.exec();
}
示例2: main
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QScriptEngine engine;
QFile scriptFile(":/object.js");
scriptFile.open(QFile::ReadOnly);
engine.evaluate(scriptFile.readAll());
scriptFile.close();
QTextEdit editor;
QTimer timer;
QScriptValue constructor = engine.evaluate("Object");
QScriptValueList arguments;
arguments << engine.newQObject(&timer);
arguments << engine.newQObject(&editor);
QScriptValue object = constructor.construct(arguments);
if (engine.hasUncaughtException()) {
qDebug() << engine.uncaughtException().toString();
}
editor.show();
timer.start(1000);
return app.exec();
}
示例3: throwError
void CodeClass::throwError(QScriptContext *context, QScriptEngine *engine, const QString &errorType, const QString &message, const QString &parent)
{
QScriptValue errorTypeValue = engine->globalObject().property(errorType);
if(!errorTypeValue.isValid())
{
errorTypeValue = engine->newFunction(emptyFunction);
engine->globalObject().setProperty(errorType, errorTypeValue);
errorTypeValue.setProperty("prototype", engine->globalObject().property(parent).construct());
}
QScriptValue thrownError = errorTypeValue.construct();
thrownError.setProperty("message", message);
thrownError.setProperty("name", errorType);
context->throwValue(thrownError);
}
示例4: loadEntityScript
QScriptValue EntityTreeRenderer::loadEntityScript(EntityItem* entity) {
if (!entity) {
return QScriptValue(); // no entity...
}
EntityItemID entityID = entity->getEntityItemID();
if (_entityScripts.contains(entityID)) {
EntityScriptDetails details = _entityScripts[entityID];
// check to make sure our script text hasn't changed on us since we last loaded it
if (details.scriptText == entity->getScript()) {
return details.scriptObject; // previously loaded
}
// if we got here, then we previously loaded a script, but the entity's script value
// has changed and so we need to reload it.
_entityScripts.remove(entityID);
}
if (entity->getScript().isEmpty()) {
return QScriptValue(); // no script
}
QString scriptContents = loadScriptContents(entity->getScript());
QScriptSyntaxCheckResult syntaxCheck = QScriptEngine::checkSyntax(scriptContents);
if (syntaxCheck.state() != QScriptSyntaxCheckResult::Valid) {
qDebug() << "EntityTreeRenderer::loadEntityScript() entity:" << entityID;
qDebug() << " " << syntaxCheck.errorMessage() << ":"
<< syntaxCheck.errorLineNumber() << syntaxCheck.errorColumnNumber();
qDebug() << " SCRIPT:" << entity->getScript();
return QScriptValue(); // invalid script
}
QScriptValue entityScriptConstructor = _entitiesScriptEngine->evaluate(scriptContents);
if (!entityScriptConstructor.isFunction()) {
qDebug() << "EntityTreeRenderer::loadEntityScript() entity:" << entityID;
qDebug() << " NOT CONSTRUCTOR";
qDebug() << " SCRIPT:" << entity->getScript();
return QScriptValue(); // invalid script
}
QScriptValue entityScriptObject = entityScriptConstructor.construct();
EntityScriptDetails newDetails = { entity->getScript(), entityScriptObject };
_entityScripts[entityID] = newDetails;
return entityScriptObject; // newly constructed
}
示例5: main
int main(int argc, char **argv)
{
Q_INIT_RESOURCE(calculator);
QApplication app(argc, argv);
//! [0a]
QScriptEngine engine;
//! [0a]
#if !defined(QT_NO_SCRIPTTOOLS)
QScriptEngineDebugger debugger;
debugger.attachTo(&engine);
QMainWindow *debugWindow = debugger.standardWindow();
debugWindow->resize(1024, 640);
#endif
//! [0b]
QString scriptFileName(":/calculator.js");
QFile scriptFile(scriptFileName);
scriptFile.open(QIODevice::ReadOnly);
engine.evaluate(scriptFile.readAll(), scriptFileName);
scriptFile.close();
//! [0b]
//! [1]
QUiLoader loader;
QFile uiFile(":/calculator.ui");
uiFile.open(QIODevice::ReadOnly);
QWidget *ui = loader.load(&uiFile);
uiFile.close();
//! [1]
//! [2]
QScriptValue ctor = engine.evaluate("Calculator");
QScriptValue scriptUi = engine.newQObject(ui, QScriptEngine::ScriptOwnership);
QScriptValue calc = ctor.construct(QScriptValueList() << scriptUi);
//! [2]
#if !defined(QT_NO_SCRIPTTOOLS)
QLineEdit *display = qFindChild<QLineEdit*>(ui, "display");
QObject::connect(display, SIGNAL(returnPressed()),
debugWindow, SLOT(show()));
#endif
//! [3]
ui->show();
return app.exec();
//! [3]
}
示例6: calledAsConstructor
void tst_QScriptContext::calledAsConstructor()
{
QScriptEngine eng;
QScriptValue fun1 = eng.newFunction(storeCalledAsConstructor);
{
fun1.call();
QVERIFY(!fun1.property("calledAsConstructor").toBool());
fun1.construct();
QVERIFY(fun1.property("calledAsConstructor").toBool());
}
{
QScriptValue fun = eng.newFunction(storeCalledAsConstructorV2, (void*)0);
fun.call();
QVERIFY(!fun.property("calledAsConstructor").toBool());
fun.construct();
QVERIFY(fun.property("calledAsConstructor").toBool());
}
{
eng.globalObject().setProperty("fun1", fun1);
eng.evaluate("fun1();");
QVERIFY(!fun1.property("calledAsConstructor").toBool());
eng.evaluate("new fun1();");
QVERIFY(fun1.property("calledAsConstructor").toBool());
}
{
QScriptValue fun3 = eng.newFunction(storeCalledAsConstructorV3);
eng.globalObject().setProperty("fun3", fun3);
eng.evaluate("function test() { fun3() }");
eng.evaluate("test();");
QVERIFY(!fun3.property("calledAsConstructor").toBool());
eng.evaluate("new test();");
if (qt_script_isJITEnabled())
QEXPECT_FAIL("", "QTBUG-6132: calledAsConstructor is not correctly set for JS functions when JIT is enabled", Continue);
QVERIFY(fun3.property("calledAsConstructor").toBool());
}
}
示例7: loadEntityScript
QScriptValue EntityTreeRenderer::loadEntityScript(EntityItem* entity) {
if (!entity) {
return QScriptValue(); // no entity...
}
// NOTE: we keep local variables for the entityID and the script because
// below in loadScriptContents() it's possible for us to execute the
// application event loop, which may cause our entity to be deleted on
// us. We don't really need access the entity after this point, can
// can accomplish all we need to here with just the script "text" and the ID.
EntityItemID entityID = entity->getEntityItemID();
QString entityScript = entity->getScript();
if (_entityScripts.contains(entityID)) {
EntityScriptDetails details = _entityScripts[entityID];
// check to make sure our script text hasn't changed on us since we last loaded it
if (details.scriptText == entityScript) {
return details.scriptObject; // previously loaded
}
// if we got here, then we previously loaded a script, but the entity's script value
// has changed and so we need to reload it.
_entityScripts.remove(entityID);
}
if (entityScript.isEmpty()) {
return QScriptValue(); // no script
}
bool isURL = false; // loadScriptContents() will tell us if this is a URL or just text.
QString scriptContents = loadScriptContents(entityScript, isURL);
QScriptSyntaxCheckResult syntaxCheck = QScriptEngine::checkSyntax(scriptContents);
if (syntaxCheck.state() != QScriptSyntaxCheckResult::Valid) {
qDebug() << "EntityTreeRenderer::loadEntityScript() entity:" << entityID;
qDebug() << " " << syntaxCheck.errorMessage() << ":"
<< syntaxCheck.errorLineNumber() << syntaxCheck.errorColumnNumber();
qDebug() << " SCRIPT:" << entityScript;
return QScriptValue(); // invalid script
}
if (isURL) {
_entitiesScriptEngine->setParentURL(entity->getScript());
}
QScriptValue entityScriptConstructor = _sandboxScriptEngine->evaluate(scriptContents);
if (!entityScriptConstructor.isFunction()) {
qDebug() << "EntityTreeRenderer::loadEntityScript() entity:" << entityID;
qDebug() << " NOT CONSTRUCTOR";
qDebug() << " SCRIPT:" << entityScript;
return QScriptValue(); // invalid script
} else {
entityScriptConstructor = _entitiesScriptEngine->evaluate(scriptContents);
}
QScriptValue entityScriptObject = entityScriptConstructor.construct();
EntityScriptDetails newDetails = { entityScript, entityScriptObject };
_entityScripts[entityID] = newDetails;
if (isURL) {
_entitiesScriptEngine->setParentURL("");
}
return entityScriptObject; // newly constructed
}
示例8: extension
//.........这里部分代码省略.........
}
// From JS
{
QScriptValue ret = eng.evaluate("obj()");
QVERIFY(ret.isObject());
QVERIFY(ret.strictlyEquals(obj));
}
cls.setCallableMode(TestClass::CallableReturnsArgumentsObject);
// From C++
{
QScriptValue ret = obj.call(obj, QScriptValueList() << 123);
QVERIFY(ret.isObject());
QVERIFY(ret.property("length").isNumber());
QCOMPARE(ret.property("length").toInt32(), 1);
QVERIFY(ret.property(0).isNumber());
QCOMPARE(ret.property(0).toInt32(), 123);
}
// From JS
{
QScriptValue ret = eng.evaluate("obj(123)");
QVERIFY(ret.isObject());
QVERIFY(ret.property("length").isNumber());
QCOMPARE(ret.property("length").toInt32(), 1);
QVERIFY(ret.property(0).isNumber());
QCOMPARE(ret.property(0).toInt32(), 123);
}
// construct()
// From C++
cls.clearReceivedArgs();
cls.setCallableMode(TestClass::CallableReturnsGlobalObject);
{
QScriptValue ret = obj.construct();
QCOMPARE(cls.lastExtensionType(), QScriptClass::Callable);
QCOMPARE(cls.lastExtensionArgument().userType(), qMetaTypeId<QScriptContext*>());
QVERIFY(ret.isObject());
QVERIFY(ret.strictlyEquals(eng.globalObject()));
}
// From JS
cls.clearReceivedArgs();
{
QScriptValue ret = eng.evaluate("new obj()");
QCOMPARE(cls.lastExtensionType(), QScriptClass::Callable);
QCOMPARE(cls.lastExtensionArgument().userType(), qMetaTypeId<QScriptContext*>());
QVERIFY(ret.isObject());
QVERIFY(ret.strictlyEquals(eng.globalObject()));
}
// From C++
cls.clearReceivedArgs();
cls.setCallableMode(TestClass::CallableInitializesThisObject);
{
QScriptValue ret = obj.construct();
QCOMPARE(cls.lastExtensionType(), QScriptClass::Callable);
QCOMPARE(cls.lastExtensionArgument().userType(), qMetaTypeId<QScriptContext*>());
QVERIFY(ret.isQObject());
QCOMPARE(ret.toQObject(), (QObject*)&eng);
}
// From JS
cls.clearReceivedArgs();
{
QScriptValue ret = eng.evaluate("new obj()");
QCOMPARE(cls.lastExtensionType(), QScriptClass::Callable);
QCOMPARE(cls.lastExtensionArgument().userType(), qMetaTypeId<QScriptContext*>());
QVERIFY(ret.isQObject());
QCOMPARE(ret.toQObject(), (QObject*)&eng);
示例9: tests
void ScriptRunner::tests()
{
QFile file(m_testScriptFileName);
if (m_doExit) return;
TestFunctionResult *tf = m_resultLogger->getTestFunctionResult("initTestCase");
if (!tf)
tf = m_resultLogger->createTestFunctionResult("initTestCase");
if(file.open(QIODevice::ReadOnly)) {
QString scriptContent = file.readAll();
if (scriptContent.length() <= 0) {
tf->addError("Can't evaluate empty script from file: '" + m_testScriptFileName +"'");
stop();
return;
}
QScriptSyntaxCheckResult result = QScriptEngine::checkSyntax(scriptContent);
if (result.state() != QScriptSyntaxCheckResult::Valid)
{
QString err = "Can't evaluate script content from file. Check syntax of script on file: '" + m_testScriptFileName +"'"
+ " Error: " + result.errorMessage()
+ " line: " + result.errorLineNumber()
+ " column: " + result.errorColumnNumber();
tf->addError(err);
stop();
return;
}
if (m_doExit) return;
QScriptValue val = m_engine->evaluate(scriptContent, m_testScriptFileName);
if(m_engine->hasUncaughtException()) {
QString err = "Can't evaluate script content from file. Check syntax of script on file: '" + m_testScriptFileName + "'"
+ " Error: " + m_engine->uncaughtExceptionBacktrace().join(" ")
+ " at line " + m_engine->uncaughtExceptionLineNumber();
tf->addError(err);
stop();
return;
}
}
else {
stop();
tf->addError("Failed to read script from file '" + m_testScriptFileName +"'");
return;
}
if (m_doExit) return;
QScriptValue ctor = m_engine->evaluate("Tests");
QScriptValue script = m_engine->newQObject(this, QScriptEngine::QtOwnership);
QScriptValue scripttests = ctor.construct(QScriptValueList() << script);
if(m_engine->hasUncaughtException()) {
QString err = "Can't evaluate script content from file. Check syntax of script on file: '" + m_testScriptFileName +"'"
+ " Error: " + m_engine->uncaughtExceptionBacktrace().join(" ");
tf->addError(err);
stop();
return;
}
}