本文整理汇总了C++中QScriptContext::setThisObject方法的典型用法代码示例。如果您正苦于以下问题:C++ QScriptContext::setThisObject方法的具体用法?C++ QScriptContext::setThisObject怎么用?C++ QScriptContext::setThisObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QScriptContext
的用法示例。
在下文中一共展示了QScriptContext::setThisObject方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: run
static QScriptValue run(QScriptEngine* engine, const QString& fileName, bool recursive)
{
QFile file(fileName);
if (file.open(QIODevice::ReadOnly)) {
QTextStream stream(&file);
const QString sourceCode = stream.readAll();
if ( recursive ) {
// A script context is pushed by the include function call.
// Link that context to the one of the script that called include
// Only do that, however, when called from the scipt
QScriptContext *context = engine->currentContext();
QScriptContext *parent=context->parentContext();
context->setActivationObject(parent->activationObject());
context->setThisObject(parent->thisObject());
}
QScriptValue result = engine->evaluate(sourceCode, fileName);
if (engine->hasUncaughtException() && errorFileName.isNull() )
errorFileName = fileName;
return result;
}
else {
Core::MessageManager::instance()->printToOutputPane(QObject::tr("Error: %1 doesn't exist.\n").arg(fileName),
Core::MessageManager::Flash);
engine->abortEvaluation();
return QScriptValue();
}
}
示例2: PushThis
void ScriptInterface::PushThis( QString thisName )
{
// Find our object, then set it as 'this' in our new context
auto thisObject = m_global.property( thisName );
QScriptContext* context = m_scriptEngine.pushContext( );
context->setThisObject( thisObject );
}
示例3: RunScript
void ScriptInterface::RunScript( QObject* obj, QString filename )
{
QScriptContext* context = m_scriptEngine.pushContext( );
context->setThisObject( m_scriptEngine.toScriptValue( obj ) );
RunScript( filename );
PopThis( );
}
示例4: IncludeFile
void JavascriptInstance::IncludeFile(const QString &path)
{
for(uint i = 0; i < includedFiles.size(); ++i)
if (includedFiles[i].toLower() == path.toLower())
{
LogDebug("JavascriptInstance::IncludeFile: Not including already included file " + path);
return;
}
QString script = LoadScript(path);
QScriptContext *context = engine_->currentContext();
assert(context);
if (!context)
{
LogError("JavascriptInstance::IncludeFile: QScriptEngine::currentContext() returned null!");
return;
}
QScriptContext *parent = context->parentContext();
if (!parent)
{
LogError("JavascriptInstance::IncludeFile: QScriptEngine::parentContext() returned null!");
return;
}
context->setActivationObject(context->parentContext()->activationObject());
context->setThisObject(context->parentContext()->thisObject());
QScriptSyntaxCheckResult syntaxResult = engine_->checkSyntax(script);
if(syntaxResult.state() != QScriptSyntaxCheckResult::Valid)
{
LogError("JavascriptInstance::IncludeFile: Syntax error in " + path + ". " + syntaxResult.errorMessage() +
" In line:" + QString::number(syntaxResult.errorLineNumber()));
return;
}
QScriptValue result = engine_->evaluate(script, path);
includedFiles.push_back(path);
if (engine_->hasUncaughtException())
LogError(result.toString());
}
示例5: handleRequest
bool HttpHandlerQtScriptFile::handleRequest(Pillow::HttpConnection *request)
{
if (!_lastModified.isValid() || (_autoReload && QFileInfo(_fileName).lastModified() > _lastModified))
{
// Time to (re)load the script.
qDebug() << "HttpHandlerQtScriptFile::handleRequest: (re)loading" << _fileName;
QFile file(_fileName);
if (!file.open(QIODevice::ReadOnly))
{
request->writeResponseString(500, HttpHeaderCollection(), QString("HttpHandlerQtScriptFile::handleRequest: Could not read file %1").arg(_fileName));
return true;
}
QScriptEngine* engine = _scriptObject.engine();
_scriptObject = engine->newObject();
QScriptContext* context = engine->pushContext();
context->setActivationObject(_scriptObject);
context->setThisObject(_scriptObject);
QScriptValue result = engine->evaluate(file.readAll(), _fileName);
engine->popContext();
if (result.isError())
{
request->writeResponseString(500, HttpHeaderCollection(), QString("HttpHandlerQtScriptFile::handleRequest: Error while evaluating script %1: %2").arg(_fileName).arg(objectToString(result)));
return true;
}
QScriptValue scriptFunction = _scriptObject.property(_functionName);
if (!scriptFunction.isFunction())
{
request->writeResponseString(500, HttpHeaderCollection(), QString("HttpHandlerQtScriptFile::handleRequest: Error while evaluating script %1: '%2' is not a function defined in the script").arg(_fileName).arg(_functionName));
return true;
}
_lastModified = QFileInfo(_fileName).lastModified();
setScriptFunction(scriptFunction);
}
return HttpHandlerQtScript::handleRequest(request);
}