当前位置: 首页>>代码示例>>C++>>正文


C++ QScriptEngine::evaluate方法代码示例

本文整理汇总了C++中QScriptEngine::evaluate方法的典型用法代码示例。如果您正苦于以下问题:C++ QScriptEngine::evaluate方法的具体用法?C++ QScriptEngine::evaluate怎么用?C++ QScriptEngine::evaluate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在QScriptEngine的用法示例。


在下文中一共展示了QScriptEngine::evaluate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main(int argc, char *argv[])
{
    Q_INIT_RESOURCE(tetrix);

//! [1]
    QApplication app(argc, argv);
    QScriptEngine engine;

    QScriptValue Qt = engine.newQMetaObject(QtMetaObject::get());
    Qt.setProperty("App", engine.newQObject(&app));
    engine.globalObject().setProperty("Qt", Qt);
//! [1]

#if !defined(QT_NO_SCRIPTTOOLS)
    QScriptEngineDebugger debugger;
    debugger.attachTo(&engine);
    QMainWindow *debugWindow = debugger.standardWindow();
    debugWindow->resize(1024, 640);
#endif

//! [2]
    evaluateFile(engine, ":/tetrixpiece.js");
    evaluateFile(engine, ":/tetrixboard.js");
    evaluateFile(engine, ":/tetrixwindow.js");
//! [2]

//! [3]
    TetrixUiLoader loader;
    QFile uiFile(":/tetrixwindow.ui");
    uiFile.open(QIODevice::ReadOnly);
    QWidget *ui = loader.load(&uiFile);
    uiFile.close();

    QScriptValue ctor = engine.evaluate("TetrixWindow");
    QScriptValue scriptUi = engine.newQObject(ui, QScriptEngine::ScriptOwnership);
    QScriptValue tetrix = ctor.construct(QScriptValueList() << scriptUi);
//! [3]

    QPushButton *debugButton = qFindChild<QPushButton*>(ui, "debugButton");
#if !defined(QT_NO_SCRIPTTOOLS)
    QObject::connect(debugButton, SIGNAL(clicked()),
                     debugger.action(QScriptEngineDebugger::InterruptAction),
                     SIGNAL(triggered()));
    QObject::connect(debugButton, SIGNAL(clicked()),
                     debugWindow, SLOT(show()));
#else
    debugButton->hide();
#endif

//! [4]
    ui->resize(550, 370);
    ui->show();

    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    return app.exec();
//! [4]
}
开发者ID:cedrus,项目名称:qt4,代码行数:57,代码来源:main.cpp

示例2: main

// Perhaps shpw entire example for getting debugger up with script
int main(int argv, char **args)
{
    QApplication app(argv, args);

     QString fileName("helloscript.qs");
     QFile scriptFile(fileName);
     scriptFile.open(QIODevice::ReadOnly);
     QTextStream stream(&scriptFile);
     QString contents = stream.readAll();
     scriptFile.close();

    QScriptEngine *engine = new QScriptEngine();

    QScriptEngineDebugger *debugger = new QScriptEngineDebugger();
    debugger->attachTo(engine);

    // Set up configuration with only stack and code
    QWidget *widget = new QWidget;
//![0]
    QWidget *codeWindow = debugger->widget(QScriptEngineDebugger::CodeWidget);
    QWidget *stackWidget = debugger->widget(QScriptEngineDebugger::StackWidget);

    QLayout *layout = new QHBoxLayout;
    layout->addWidget(codeWindow);
    layout->addWidget(stackWidget);
//![0]

//![1]
    QAction *continueAction = debugger->action(QScriptEngineDebugger::ContinueAction);
    QAction *stepOverAction = debugger->action(QScriptEngineDebugger::StepOverAction);
    QAction *stepIntoAction = debugger->action(QScriptEngineDebugger::StepIntoAction);

    QToolBar *toolBar = new QToolBar;
    toolBar->addAction(continueAction);
//![1]
    toolBar->addAction(stepOverAction);
    toolBar->addAction(stepIntoAction);

    layout->addWidget(toolBar);
    continueAction->setIcon(QIcon("copy.png"));

    debugger->setAutoShowStandardWindow(false);

    widget->setLayout(layout);
    widget->show();

     QPushButton button;
     QScriptValue scriptButton = engine->newQObject(&button);
     engine->globalObject().setProperty("button", scriptButton);

//![2]
     debugger->action(QScriptEngineDebugger::InterruptAction)->trigger();
     engine->evaluate(contents, fileName);
//![2]

    return app.exec();
}
开发者ID:maxxant,项目名称:qt,代码行数:58,代码来源:scriptdebugger.cpp

示例3: getParentContextCallee

void tst_QScriptContext::parentContextCallee_QT2270()
{
    QScriptEngine engine;
    engine.globalObject().setProperty("getParentContextCallee", engine.newFunction(getParentContextCallee));
    QScriptValue fun = engine.evaluate("(function() { return getParentContextCallee(); })");
    QVERIFY(fun.isFunction());
    QScriptValue callee = fun.call();
    QVERIFY(callee.equals(fun));
}
开发者ID:kuailexs,项目名称:symbiandump-mw3,代码行数:9,代码来源:tst_qscriptcontext.cpp

示例4: parseRetrievedMessages

QVariantList parseRetrievedMessages(const QByteArray &result)
{
    std::cout << QString(result).toStdString() << std::endl;

	QVariantList list;
    QScriptEngine engine;
    QScriptValue response = engine.evaluate("(" + QString(result) + ")").property(RESPONSE);
    QScriptValueIterator it(response);

    while (it.hasNext()) {
        it.next();

        // Parse message id
        QString messageId = it.value().property(MESSAGE_ID).toString();

        // Parse message text
        QByteArray rawText = it.value().property(MESSAGE_TEXT).toVariant().toByteArray();
        QString messageText = QString::fromUtf8(rawText, rawText.size());

        // Parse message date
        QString messageDate = QDateTime::fromTime_t(it.value().property(MESSAGE_DATE).toUInt32()).toString(DATE_TIME_FORMAT);

        // Parse likes, comments and reposts
        int likes = it.value().property(MESSAGE_LIKES).property(COUNT).toInt32();
        int comments = it.value().property(MESSAGE_COMMENTS).property(COUNT).toInt32();
        int reposts = it.value().property(MESSAGE_REPOSTS).property(COUNT).toInt32();

        // Online status
        bool online = it.value().property(IS_ONLINE).toNumber() == ONLINE;

        if (!messageId.isEmpty() && !messageText.isEmpty() && !messageDate.isEmpty())
        {
        	QVariantMap message;

        	message.insert(MESSAGE_ID, messageId);
            message.insert(MESSAGE_TEXT, messageText);
            message.insert(MESSAGE_DATE,messageDate);

            if (likes > 0)
            	message.insert(MESSAGE_LIKES, likes);

            if (comments > 0)
            	message.insert(MESSAGE_COMMENTS, comments);

            if (reposts > 0)
            	message.insert(MESSAGE_REPOSTS, reposts);

            if (online)
            	message.insert(IS_ONLINE, ONLINE);

            list.append(message);
        }

    }
    return list;
}
开发者ID:andrlee,项目名称:vkplayer,代码行数:56,代码来源:vkontaktemessages.cpp

示例5: encode

QString Json::encode(const QMap<QString,QVariant> &map)
{
    QScriptEngine engine;
    engine.evaluate("function toString() { return JSON.stringify(this) }");

    QScriptValue toString = engine.globalObject().property("toString");
    QScriptValue obj = encodeInner(map, &engine);
    return toString.call(obj).toString();

}
开发者ID:magnesj,项目名称:ResInsight,代码行数:10,代码来源:RifJsonEncodeDecode.cpp

示例6: popNativeContextScope

void tst_QScriptContext::popNativeContextScope()
{
    QScriptEngine eng;
    QScriptContext *ctx = eng.pushContext();
    QVERIFY(ctx->popScope().isObject()); // the activation object

    QCOMPARE(ctx->scopeChain().size(), 1);
    QVERIFY(ctx->scopeChain().at(0).strictlyEquals(eng.globalObject()));
    // This was different in 4.5: scope and activation were decoupled
    QVERIFY(ctx->activationObject().strictlyEquals(eng.globalObject()));

    QVERIFY(!eng.evaluate("var foo = 123; function bar() {}").isError());
    QVERIFY(eng.globalObject().property("foo").isNumber());
    QVERIFY(eng.globalObject().property("bar").isFunction());

    QScriptValue customScope = eng.newObject();
    ctx->pushScope(customScope);
    QCOMPARE(ctx->scopeChain().size(), 2);
    QVERIFY(ctx->scopeChain().at(0).strictlyEquals(customScope));
    QVERIFY(ctx->scopeChain().at(1).strictlyEquals(eng.globalObject()));
    QVERIFY(ctx->activationObject().strictlyEquals(eng.globalObject()));
    ctx->setActivationObject(customScope);
    QVERIFY(ctx->activationObject().strictlyEquals(customScope));
    QCOMPARE(ctx->scopeChain().size(), 2);
    QVERIFY(ctx->scopeChain().at(0).strictlyEquals(customScope));
    QEXPECT_FAIL("", "QTBUG-11012", Continue);
    QVERIFY(ctx->scopeChain().at(1).strictlyEquals(eng.globalObject()));

    QVERIFY(!eng.evaluate("baz = 456; var foo = 789; function barbar() {}").isError());
    QEXPECT_FAIL("", "QTBUG-11012", Continue);
    QVERIFY(eng.globalObject().property("baz").isNumber());
    QVERIFY(customScope.property("foo").isNumber());
    QVERIFY(customScope.property("barbar").isFunction());

    QVERIFY(ctx->popScope().strictlyEquals(customScope));
    QCOMPARE(ctx->scopeChain().size(), 1);
    QEXPECT_FAIL("", "QTBUG-11012", Continue);
    QVERIFY(ctx->scopeChain().at(0).strictlyEquals(eng.globalObject()));

    // Need to push another object, otherwise we crash in popContext() (QTBUG-11012)
    ctx->pushScope(customScope);
    eng.popContext();
}
开发者ID:kuailexs,项目名称:symbiandump-mw3,代码行数:43,代码来源:tst_qscriptcontext.cpp

示例7:

void MainWindow::on_pushButton_2_clicked()
{
    QString CodeFunction="2+2*2";

    QScriptEngine engine;

    double Result = engine.evaluate(CodeFunction).toNumber();

    ui->textEdit->insertPlainText(QString::number(Result)+"\n");
}
开发者ID:Harrix,项目名称:Harrix-QtExamples,代码行数:10,代码来源:mainwindow.cpp

示例8: jsActivationObject

void tst_QScriptContext::jsActivationObject()
{
    QScriptEngine eng;
    eng.globalObject().setProperty("get_jsActivationObject", eng.newFunction(get_jsActivationObject));
    eng.evaluate("function f1() { var w = get_jsActivationObject('arg1');  return w; }");
    eng.evaluate("function f2(x,y,z) { var v1 = 42;\n"
                        //   "function foo() {};\n" //this would avoid JSC to optimize
                        "var v2 = f1(); return v2; }");
    eng.evaluate("function f3() { var v1 = 'nothing'; return f2(1,2,3); }");
    QScriptValue result1 = eng.evaluate("f2('hello', 'useless', 'world')");
    QScriptValue result2 = eng.evaluate("f3()");
    QVERIFY(result1.isObject());
    QEXPECT_FAIL("", "JSC optimize away the activation object", Abort);
    QCOMPARE(result1.property("v1").toInt32() , 42);
    QCOMPARE(result1.property("arguments").property(1).toString() , QString::fromLatin1("useless"));
    QVERIFY(result2.isObject());
    QCOMPARE(result2.property("v1").toInt32() , 42);
    QCOMPARE(result2.property("arguments").property(1).toString() , QString::fromLatin1("2"));
}
开发者ID:kuailexs,项目名称:symbiandump-mw3,代码行数:19,代码来源:tst_qscriptcontext.cpp

示例9: execute

bool JSScript::execute(TWScriptAPI *tw) const
{
	QFile scriptFile(m_Filename);
	if (!scriptFile.open(QIODevice::ReadOnly)) {
		// handle error
		return false;
	}
	QTextStream stream(&scriptFile);
	stream.setCodec(m_Codec);
	QString contents = stream.readAll();
	scriptFile.close();
	
	QScriptEngine engine;
	QScriptValue twObject = engine.newQObject(tw);
	engine.globalObject().setProperty("TW", twObject);
	
	QScriptValue val;

#if QT_VERSION >= 0x040500
	QSETTINGS_OBJECT(settings);
	if (settings.value("scriptDebugger", false).toBool()) {
		QScriptEngineDebugger debugger;
		debugger.attachTo(&engine);
		val = engine.evaluate(contents, m_Filename);
	}
	else {
		val = engine.evaluate(contents, m_Filename);
	}
#else
	val = engine.evaluate(contents, m_Filename);
#endif

	if (engine.hasUncaughtException()) {
		tw->SetResult(engine.uncaughtException().toString());
		return false;
	}
	else {
		if (!val.isUndefined()) {
			tw->SetResult(convertValue(val));
		}
		return true;
	}
}
开发者ID:Berenco,项目名称:texworks,代码行数:43,代码来源:TWScriptable.cpp

示例10: getProperty_invalidValue

void tst_QScriptClass::getProperty_invalidValue()
{
    QScriptEngine eng;
    TestClass cls(&eng);
    cls.addCustomProperty(eng.toStringHandle("foo"), QScriptClass::HandlesReadAccess,
                          /*id=*/0, QScriptValue::ReadOnly, QScriptValue());
    QScriptValue obj = eng.newObject(&cls);

    QVERIFY(obj.property("foo").isUndefined());

    eng.globalObject().setProperty("obj", obj);
    QVERIFY(eng.evaluate("obj.hasOwnProperty('foo'))").toBool());
    // The JS environment expects that a valid value is returned,
    // otherwise we could crash.
    QVERIFY(eng.evaluate("obj.foo").isUndefined());
    QVERIFY(eng.evaluate("obj.foo + ''").isString());
    QVERIFY(eng.evaluate("Object.getOwnPropertyDescriptor(obj, 'foo').value").isUndefined());
    QVERIFY(eng.evaluate("Object.getOwnPropertyDescriptor(obj, 'foo').value +''").isString());
}
开发者ID:husninazer,项目名称:qt,代码行数:19,代码来源:tst_qscriptclass.cpp

示例11: toString

void tst_QScriptContext::toString()
{
    QScriptEngine eng;
    eng.globalObject().setProperty("parentContextToString", eng.newFunction(parentContextToString));
    QScriptValue ret = eng.evaluate("function foo(first, second, third) {\n"
                                    "    return parentContextToString();\n"
                                    "}; foo(1, 2, 3)", "script.qs");
    QVERIFY(ret.isString());
    QCOMPARE(ret.toString(), QString::fromLatin1("foo(first = 1, second = 2, third = 3) at script.qs:2"));
}
开发者ID:kuailexs,项目名称:symbiandump-mw3,代码行数:10,代码来源:tst_qscriptcontext.cpp

示例12: qDebug

void
StravaUploader::requestVerifyUploadFinished(QNetworkReply *reply)
{
    uploadSuccessful = false;
    if (reply->error() != QNetworkReply::NoError)
        qDebug() << "Error from upload " <<reply->error();
    else {
        QString response = reply->readLine();

        //qDebug() << response;

        QScriptValue sc;
        QScriptEngine se;

        sc = se.evaluate("("+response+")");
        uploadProgress = sc.property("upload_progress").toString();

        //qDebug() << "upload_progress: " << uploadProgress;
        parent->progressBar->setValue(uploadProgress.toInt());

        stravaActivityId = sc.property("activity_id").toString();

        if (stravaActivityId.length() == 0) {
            requestVerifyUpload();
            return;
        }

        ride->ride()->setTag("Strava activityId", stravaActivityId);
        ride->setDirty(true);

        sc = se.evaluate("("+response+")");
        uploadStatus = sc.property("upload_status").toString();

        //qDebug() << "upload_status: " << uploadStatus;
        parent->progressLabel->setText(uploadStatus);

        if (uploadProgress.toInt() < 97) {
            requestVerifyUpload();
            return;
        }
        uploadSuccessful = true;
    }
}
开发者ID:Gerryboy1964,项目名称:GoldenCheetah,代码行数:43,代码来源:ShareDialog.cpp

示例13: build

JsonValue *JsonValue::create(const QString &s, JsonMemoryPool *pool)
{
    QScriptEngine engine;
    QScriptValue jsonParser = engine.evaluate(QLatin1String("JSON.parse"));
    QScriptValue value = jsonParser.call(QScriptValue(), QScriptValueList() << s);
    if (engine.hasUncaughtException() || !value.isValid())
        return 0;

    return build(value.toVariant(), pool);
}
开发者ID:,项目名称:,代码行数:10,代码来源:

示例14: scopeChain

void tst_QScriptContext::scopeChain()
{
    QScriptEngine eng;
    {
        QScriptValueList ret = eng.currentContext()->scopeChain();
        QCOMPARE(ret.size(), 1);
        QVERIFY(ret.at(0).strictlyEquals(eng.globalObject()));
    }
    {
        eng.globalObject().setProperty("getScopeChain", eng.newFunction(getScopeChain));
        QScriptValueList ret = qscriptvalue_cast<QScriptValueList>(eng.evaluate("getScopeChain()"));
        QCOMPARE(ret.size(), 1);
        QVERIFY(ret.at(0).strictlyEquals(eng.globalObject()));
    }
    {
        eng.evaluate("function foo() { function bar() { return getScopeChain(); } return bar() }");
        QScriptValueList ret = qscriptvalue_cast<QScriptValueList>(eng.evaluate("foo()"));
        QEXPECT_FAIL("", "Number of items in returned scope chain is incorrect", Abort);
        QCOMPARE(ret.size(), 3);
        QVERIFY(ret.at(2).strictlyEquals(eng.globalObject()));
        QCOMPARE(ret.at(1).toString(), QString::fromLatin1("activation"));
        QVERIFY(ret.at(1).property("arguments").isObject());
        QCOMPARE(ret.at(0).toString(), QString::fromLatin1("activation"));
        QVERIFY(ret.at(0).property("arguments").isObject());
    }
    {
        QScriptValueList ret = qscriptvalue_cast<QScriptValueList>(eng.evaluate("o = { x: 123 }; with(o) getScopeChain();"));
        QCOMPARE(ret.size(), 2);
        QVERIFY(ret.at(1).strictlyEquals(eng.globalObject()));
        QVERIFY(ret.at(0).isObject());
        QCOMPARE(ret.at(0).property("x").toInt32(), 123);
    }
    {
        QScriptValueList ret = qscriptvalue_cast<QScriptValueList>(
            eng.evaluate("o1 = { x: 123}; o2 = { y: 456 }; with(o1) { with(o2) { getScopeChain(); } }"));
        QCOMPARE(ret.size(), 3);
        QVERIFY(ret.at(2).strictlyEquals(eng.globalObject()));
        QVERIFY(ret.at(1).isObject());
        QCOMPARE(ret.at(1).property("x").toInt32(), 123);
        QVERIFY(ret.at(0).isObject());
        QCOMPARE(ret.at(0).property("y").toInt32(), 456);
    }
}
开发者ID:kuailexs,项目名称:symbiandump-mw3,代码行数:43,代码来源:tst_qscriptcontext.cpp

示例15: replyFinished

void AddWindow::replyFinished(QNetworkReply *reply)
{
    QVariant statusCodev = reply->readAll();
    str = statusCodev.toString();
    QScriptValue sc;
    QScriptEngine engine;
    sc=engine.evaluate("value="+str);
    QScriptValueIterator it(sc);
    it.next();
    QString Id=it.value().toString();
    if(Id=="Parse error")
    {
        ui->textBrowser->clear();
        ui->label_2->hide();
        ui->faceButton->hide();
        ui->label_4->hide();
        ui->pushButton_2->hide();
        QString c="不存在该用户,请重新输入";
        ui->textBrowser->setText(c);
    }
    else{
        ui->textBrowser->clear();
        it.next();
        it.next();
        QString nickname=it.value().toString();
        ui->label_2->setText(nickname);
        it.next();
        //头像处理
        QByteArray head,face;
                QString str(it.value().toString());
                int j = str.indexOf('-');
                while(j!=-1){
                    str[j] = '+';
                    j = str.indexOf('-');
                }
                head = QByteArray::fromBase64(str.toLatin1());
                face = qUncompress(head);
                QImage img;
                img.loadFromData(face);
                ui->faceButton->setIcon(QPixmap::fromImage(img));


        it.next();
        QString signature=it.value().toString();
        ui->label_4->setText(signature);
        ui->label_2->show();
        ui->faceButton->show();
        ui->label_4->show();
        ui->pushButton_2->show();
    }
    /*while (it.hasNext()){
        it.next();
        qDebug()<<it.name()<<":"<<it.value().toString();
    }*/
}
开发者ID:macinchang,项目名称:ichat,代码行数:55,代码来源:addwindow.cpp


注:本文中的QScriptEngine::evaluate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。