QScriptEngine

概要

函数

信号

静态函数

详细描述

PySide.QtScript.QScriptEngine class provides an environment for evaluating Qt Script code.

QtScript documentation for information about the Qt Script language, and how to get started with scripting your C++ application.

Evaluating Scripts

使用 PySide.QtScript.QScriptEngine.evaluate() to evaluate script code; this is the C++ equivalent of the built-in script function eval() .

myEngine = QScriptEngine()
three = myEngine.evaluate("1 + 2")
											

PySide.QtScript.QScriptEngine.evaluate() 返回 PySide.QtScript.QScriptValue that holds the result of the evaluation. The PySide.QtScript.QScriptValue class provides functions for converting the result to various C++ types (e.g. QScriptValue.toString() and QScriptValue.toNumber() ).

The following code snippet shows how a script function can be defined and then invoked from C++ using QScriptValue.call() :

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

As can be seen from the above snippets, a script is provided to the engine in the form of a string. One common way of loading scripts is by reading the contents of a file and passing it to PySide.QtScript.QScriptEngine.evaluate() :

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

Here we pass the name of the file as the second argument to PySide.QtScript.QScriptEngine.evaluate() . This does not affect evaluation in any way; the second argument is a general-purpose string that is used to identify the script for debugging purposes (for example, our filename will now show up in any PySide.QtScript.QScriptEngine.uncaughtExceptionBacktrace() involving the script).

Engine Configuration

PySide.QtScript.QScriptEngine.globalObject() function returns the Global Object associated with the script engine. Properties of the Global Object are accessible from any script code (i.e. they are global variables). Typically, before evaluating “user” scripts, you will want to configure a script engine by adding one or more properties to the Global Object:

myEngine.globalObject().setProperty("myNumber", 123)
...
myNumberPlusOne = myEngine.evaluate("myNumber + 1")
											

Adding custom properties to the scripting environment is one of the standard means of providing a scripting API that is specific to your application. Usually these custom properties are objects created by the PySide.QtScript.QScriptEngine.newQObject() or PySide.QtScript.QScriptEngine.newObject() functions, or constructor functions created by newFunction() .

Script Exceptions

PySide.QtScript.QScriptEngine.evaluate() can throw a script exception (e.g. due to a syntax error); in that case, the return value is the value that was thrown (typically an Error object). You can check whether the evaluation caused an exception by calling PySide.QtScript.QScriptEngine.hasUncaughtException() . In that case, you can call toString() on the error object to obtain an error message. The current uncaught exception is also available through PySide.QtScript.QScriptEngine.uncaughtException() . Calling PySide.QtScript.QScriptEngine.clearExceptions() will cause any uncaught exceptions to be cleared.

result = myEngine.evaluate(...)
if myEngine.hasUncaughtException():
    line = myEngine.uncaughtExceptionLineNumber()
    print "uncaught exception at line", line, ":", result.toString()
											

checkSyntax() function can be used to determine whether code can be usefully passed to PySide.QtScript.QScriptEngine.evaluate() .

Script Object Creation

使用 PySide.QtScript.QScriptEngine.newObject() to create a standard Qt Script object; this is the C++ equivalent of the script statement new Object() . You can use the object-specific functionality in PySide.QtScript.QScriptValue to manipulate the script object (e.g. QScriptValue.setProperty() ). Similarly, use PySide.QtScript.QScriptEngine.newArray() to create a Qt Script array object. Use PySide.QtScript.QScriptEngine.newDate() to create a Date object, and PySide.QtScript.QScriptEngine.newRegExp() to create a RegExp 对象。

QObject Integration

使用 PySide.QtScript.QScriptEngine.newQObject() to wrap a PySide.QtCore.QObject (or subclass) pointer. PySide.QtScript.QScriptEngine.newQObject() returns a proxy script object; properties, children, and signals and slots of the PySide.QtCore.QObject are available as properties of the proxy object. No binding code is needed because it is done dynamically using the Qt meta object system.

button = QPushButton()
QScriptValue scriptButton = myEngine.QObject(button)
myEngine.globalObject().setProperty("button", scriptButton)
myEngine.evaluate("button.checkable = True")
print scriptButton.property("checkable").toBoolean()
scriptButton.property("show").call() # call the show() slot
										

使用 qScriptConnect() to connect a C++ signal to a script function; this is the Qt Script equivalent of QObject.connect() . When a script function is invoked in response to a C++ signal, it can cause a script exception; you can connect to the PySide.QtScript.QScriptEngine.signalHandlerException() signal to catch such an exception.

使用 PySide.QtScript.QScriptEngine.newQMetaObject() to wrap a PySide.QtCore.QMetaObject ; this gives you a “script representation” of a PySide.QtCore.QObject -based class. PySide.QtScript.QScriptEngine.newQMetaObject() returns a proxy script object; enum values of the class are available as properties of the proxy object. You can also specify a function that will be used to construct objects of the class (e.g. when the constructor is invoked from a script). For classes that have a “standard” Qt constructor, Qt Script can provide a default script constructor for you; see PySide.QtScript.QScriptEngine.scriptValueFromQMetaObject() .

For more information about PySide.QtCore.QObject integration, see Making Applications Scriptable

Support for Custom C++ Types

使用 PySide.QtScript.QScriptEngine.newVariant() to wrap a PySide.QtCore.QVariant . This can be used to store values of custom (non- PySide.QtCore.QObject ) C++ types that have been registered with the Qt meta-type system. To make such types scriptable, you typically associate a prototype (delegate) object with the C++ type by calling PySide.QtScript.QScriptEngine.setDefaultPrototype() ; the prototype object defines the scripting API for the C++ type. Unlike the PySide.QtCore.QObject integration, there is no automatic binding possible here; i.e. you have to create the scripting API yourself, for example by using the PySide.QtScript.QScriptable 类。

使用 fromScriptValue() to cast from a PySide.QtScript.QScriptValue to another type, and toScriptValue() to create a PySide.QtScript.QScriptValue from another value. You can specify how the conversion of C++ types is to be performed with qScriptRegisterMetaType() and qScriptRegisterSequenceMetaType() . By default, Qt Script will use PySide.QtCore.QVariant to store values of custom types.

Importing Extensions

使用 PySide.QtScript.QScriptEngine.importExtension() to import plugin-based extensions into the engine. Call PySide.QtScript.QScriptEngine.availableExtensions() to obtain a list naming all the available extensions, and PySide.QtScript.QScriptEngine.importedExtensions() to obtain a list naming only those extensions that have been imported.

调用 PySide.QtScript.QScriptEngine.pushContext() to open up a new variable scope, and PySide.QtScript.QScriptEngine.popContext() to close the current scope. This is useful if you are implementing an extension that evaluates script code containing temporary variable definitions (e.g. var foo = 123; ) that are safe to discard when evaluation has completed.

Native Functions

使用 newFunction() to wrap native (C++) functions, including constructors for your own custom types, so that these can be invoked from script code. Such functions must have the signature QScriptEngine.FunctionSignature . You may then pass the function as argument to newFunction() . Here is an example of a function that returns the sum of its first two arguments:

def myAdd(context, engine):
   a = context.argument(0)
   b = context.argument(1)
   return a.toNumber() + b.toNumber()
											

To expose this function to script code, you can set it as a property of the Global Object:

fun = myEngine.Function(myAdd)
myEngine.globalObject().setProperty("myAdd", fun)
											

Once this is done, script code can call your function in the exact same manner as a “normal” script function:

result = myEngine.evaluate("myAdd(myNumber, 1)")
											

Long-running Scripts

If you need to evaluate possibly long-running scripts from the main (GUI) thread, you should first call PySide.QtScript.QScriptEngine.setProcessEventsInterval() to make sure that the GUI stays responsive. You can abort a currently running script by calling PySide.QtScript.QScriptEngine.abortEvaluation() . You can determine whether an engine is currently running a script by calling PySide.QtScript.QScriptEngine.isEvaluating() .

垃圾收集

Qt Script objects may be garbage collected when they are no longer referenced. There is no guarantee as to when automatic garbage collection will take place.

PySide.QtScript.QScriptEngine.collectGarbage() function can be called to explicitly request garbage collection.

PySide.QtScript.QScriptEngine.reportAdditionalMemoryCost() function can be called to indicate that a Qt Script object occupies memory that isn't managed by the scripting environment. Reporting the additional cost makes it more likely that the garbage collector will be triggered. This can be useful, for example, when many custom, native Qt Script objects are allocated.

Core Debugging/Tracing Facilities

Since Qt 4.4, you can be notified of events pertaining to script execution (e.g. script function calls and statement execution) through the PySide.QtScript.QScriptEngineAgent interface; see the PySide.QtScript.QScriptEngine.setAgent() function. This can be used to implement debugging and profiling of a PySide.QtScript.QScriptEngine .

class PySide.QtScript. QScriptEngine
class PySide.QtScript. QScriptEngine ( parent )
参数: parent PySide.QtCore.QObject

构造 PySide.QtScript.QScriptEngine 对象。

PySide.QtScript.QScriptEngine.globalObject() is initialized to have properties as described in ECMA-262 , Section 15.1.

构造 PySide.QtScript.QScriptEngine 对象采用给定 parent .

PySide.QtScript.QScriptEngine.globalObject() is initialized to have properties as described in ECMA-262 , Section 15.1.

PySide.QtScript.QScriptEngine. ValueOwnership

This enum specifies the ownership when wrapping a C++ value, e.g. by using PySide.QtScript.QScriptEngine.newQObject() .

常量 描述
QScriptEngine.QtOwnership The standard Qt ownership rules apply, i.e. the associated object will never be explicitly deleted by the script engine. This is the default. ( PySide.QtCore.QObject ownership is explained in Object Trees & Ownership .)
QScriptEngine.ScriptOwnership The value is owned by the script environment. The associated data will be deleted when appropriate (i.e. after the garbage collector has discovered that there are no more live references to the value).
QScriptEngine.AutoOwnership If the associated object has a parent, the Qt ownership rules apply ( QtOwnership ); otherwise, the object is owned by the script environment ( ScriptOwnership ).
PySide.QtScript.QScriptEngine. QObjectWrapOption

These flags specify options when wrapping a PySide.QtCore.QObject pointer with PySide.QtScript.QScriptEngine.newQObject() .

常量 描述
QScriptEngine.ExcludeChildObjects The script object will not expose child objects as properties.
QScriptEngine.ExcludeSuperClassMethods The script object will not expose signals and slots inherited from the superclass.
QScriptEngine.ExcludeSuperClassProperties The script object will not expose properties inherited from the superclass.
QScriptEngine.ExcludeSuperClassContents Shorthand form for ExcludeSuperClassMethods | ExcludeSuperClassProperties
QScriptEngine.ExcludeDeleteLater The script object will not expose the QObject.deleteLater() slot.
QScriptEngine.ExcludeSlots The script object will not expose the PySide.QtCore.QObject ‘s slots.
QScriptEngine.AutoCreateDynamicProperties Properties that don't already exist in the PySide.QtCore.QObject will be created as dynamic properties of that object, rather than as properties of the script object.
QScriptEngine.PreferExistingWrapperObject If a wrapper object with the requested configuration already exists, return that object.
QScriptEngine.SkipMethodsInEnumeration Don't include methods (signals and slots) when enumerating the object's properties.
PySide.QtScript.QScriptEngine. abortEvaluation ( [ result=QScriptValue() ] )
参数: result PySide.QtScript.QScriptValue

Aborts any script evaluation currently taking place in this engine. The given result is passed back as the result of the evaluation (i.e. it is returned from the call to PySide.QtScript.QScriptEngine.evaluate() being aborted).

If the engine isn't evaluating a script (i.e. PySide.QtScript.QScriptEngine.isEvaluating() returns false), this function does nothing.

Call this function if you need to abort a running script for some reason, e.g. when you have detected that the script has been running for several seconds without completing.

PySide.QtScript.QScriptEngine. agent ( )
返回类型: PySide.QtScript.QScriptEngineAgent

Returns the agent currently installed on this engine, or 0 if no agent is installed.

PySide.QtScript.QScriptEngine. availableExtensions ( )
返回类型: list of strings

Returns a list naming the available extensions that can be imported using the PySide.QtScript.QScriptEngine.importExtension() function. This list includes extensions that have been imported.

PySide.QtScript.QScriptEngine. canEvaluate ( program )
参数: program – unicode
返回类型: PySide.QtCore.bool

Returns true if program can be evaluated; i.e. the code is sufficient to determine whether it appears to be a syntactically correct program, or contains a syntax error.

This function returns false if program is incomplete; i.e. the input is syntactically correct up to the point where the input is terminated.

Note that this function only does a static check of program ; e.g. it does not check whether references to variables are valid, and so on.

A typical usage of PySide.QtScript.QScriptEngine.canEvaluate() is to implement an interactive interpreter for QtScript . The user is repeatedly queried for individual lines of code; the lines are concatened internally, and only when PySide.QtScript.QScriptEngine.canEvaluate() returns true for the resulting program is it passed to PySide.QtScript.QScriptEngine.evaluate() .

The following are some examples to illustrate the behavior of PySide.QtScript.QScriptEngine.canEvaluate() . (Note that all example inputs are assumed to have an explicit newline as their last character, since otherwise the QtScript parser would automatically insert a semi-colon character at the end of the input, and this could cause PySide.QtScript.QScriptEngine.canEvaluate() to produce different results.)

Given the input

if hello && world:
    print("hello world")
											

PySide.QtScript.QScriptEngine.canEvaluate() will return true, since the program appears to be complete.

Given the input

if hello &&
											

PySide.QtScript.QScriptEngine.canEvaluate() will return false, since the if-statement is not complete, but is syntactically correct so far.

Given the input

0 = 0
												

PySide.QtScript.QScriptEngine.canEvaluate() will return true, but PySide.QtScript.QScriptEngine.evaluate() will throw a SyntaxError given the same input.

Given the input

./test.js
											

PySide.QtScript.QScriptEngine.canEvaluate() will return true, even though the code is clearly not syntactically valid QtScript code. PySide.QtScript.QScriptEngine.evaluate() will throw a SyntaxError when this code is evaluated.

Given the input

foo["bar"]
												

PySide.QtScript.QScriptEngine.canEvaluate() will return true, but PySide.QtScript.QScriptEngine.evaluate() will throw a ReferenceError if foo is not defined in the script environment.

PySide.QtScript.QScriptEngine. clearExceptions ( )

Clears any uncaught exceptions in this engine.

PySide.QtScript.QScriptEngine. collectGarbage ( )

Runs the garbage collector.

The garbage collector will attempt to reclaim memory by locating and disposing of objects that are no longer reachable in the script environment.

Normally you don't need to call this function; the garbage collector will automatically be invoked when the PySide.QtScript.QScriptEngine decides that it's wise to do so (i.e. when a certain number of new objects have been created). However, you can call this function to explicitly request that garbage collection should be performed as soon as possible.

PySide.QtScript.QScriptEngine. convert ( value , type , ptr )
参数:
返回类型:

PySide.QtCore.bool

static PySide.QtScript.QScriptEngine. convertV2 ( value , type , ptr )
参数:
返回类型:

PySide.QtCore.bool

PySide.QtScript.QScriptEngine. create ( type , ptr )
参数:
  • type PySide.QtCore.int
  • ptr void
返回类型:

PySide.QtScript.QScriptValue

PySide.QtScript.QScriptEngine. currentContext ( )
返回类型: PySide.QtScript.QScriptContext

Returns the current context.

The current context is typically accessed to retrieve the arguments and this' object in native functions; for convenience, it is available as the first argument in :class:`QScriptEngine.FunctionSignature<~PySide.QtScript.QScriptEngine.FunctionSignature> .

PySide.QtScript.QScriptEngine. defaultPrototype ( metaTypeId )
参数: metaTypeId PySide.QtCore.int
返回类型: PySide.QtScript.QScriptValue

Returns the default prototype associated with the given metaTypeId , or an invalid PySide.QtScript.QScriptValue if no default prototype has been set.

PySide.QtScript.QScriptEngine. evaluate ( program [ , fileName="" [ , lineNumber=1 ] ] )
参数:
  • program – unicode
  • fileName – unicode
  • lineNumber PySide.QtCore.int
返回类型:

PySide.QtScript.QScriptValue

Evaluates program ,使用 lineNumber as the base line number, and returns the result of the evaluation.

The script code will be evaluated in the current context.

The evaluation of program can cause an exception in the engine; in this case the return value will be the exception that was thrown (typically an Error object). You can call PySide.QtScript.QScriptEngine.hasUncaughtException() to determine if an exception occurred in the last call to PySide.QtScript.QScriptEngine.evaluate() .

lineNumber is used to specify a starting line number for program ; line number information reported by the engine that pertain to this evaluation (e.g. PySide.QtScript.QScriptEngine.uncaughtExceptionLineNumber() ) will be based on this argument. For example, if program consists of two lines of code, and the statement on the second line causes a script exception, PySide.QtScript.QScriptEngine.uncaughtExceptionLineNumber() would return the given lineNumber plus one. When no starting line number is specified, line numbers will be 1-based.

fileName is used for error reporting. For example in error objects the file name is accessible through the “fileName” property if it's provided with this function.

PySide.QtScript.QScriptEngine. evaluate ( program )
参数: program PySide.QtScript.QScriptProgram
返回类型: PySide.QtScript.QScriptValue

Evaluates the given program and returns the result of the evaluation.

PySide.QtScript.QScriptEngine. globalObject ( )
返回类型: PySide.QtScript.QScriptValue

Returns this engine's Global Object.

By default, the Global Object contains the built-in objects that are part of ECMA-262 , such as Math, Date and String. Additionally, you can set properties of the Global Object to make your own extensions available to all script code. Non-local variables in script code will be created as properties of the Global Object, as well as local variables in global code.

PySide.QtScript.QScriptEngine. hasUncaughtException ( )
返回类型: PySide.QtCore.bool

Returns true if the last script evaluation resulted in an uncaught exception; otherwise returns false.

The exception state is cleared when PySide.QtScript.QScriptEngine.evaluate() 被调用。

PySide.QtScript.QScriptEngine. importExtension ( extension )
参数: extension – unicode
返回类型: PySide.QtScript.QScriptValue

Imports the given extension into this PySide.QtScript.QScriptEngine 。返回 PySide.QtScript.QScriptEngine.undefinedValue() if the extension was successfully imported. You can call PySide.QtScript.QScriptEngine.hasUncaughtException() to check if an error occurred; in that case, the return value is the value that was thrown by the exception (usually an Error 对象)。

PySide.QtScript.QScriptEngine ensures that a particular extension is only imported once; subsequent calls to PySide.QtScript.QScriptEngine.importExtension() with the same extension name will do nothing and return PySide.QtScript.QScriptEngine.undefinedValue() .

PySide.QtScript.QScriptEngine. importedExtensions ( )
返回类型: list of strings

Returns a list naming the extensions that have been imported using the PySide.QtScript.QScriptEngine.importExtension() 函数。

PySide.QtScript.QScriptEngine. installTranslatorFunctions ( [ object=QScriptValue() ] )
参数: object PySide.QtScript.QScriptValue

Installs translator functions on the given object , or on the Global Object if no object is specified.

The relation between Qt Script translator functions and C++ translator functions is described in the following table:

Script Function Corresponding C++ Function
qsTr() QObject.tr()
QT_TR_NOOP() QT_TR_NOOP()
qsTranslate() QCoreApplication.translate()
QT_TRANSLATE_NOOP() QT_TRANSLATE_NOOP()
qsTrId() (since 4.7) qtTrId()
QT_TRID_NOOP() (since 4.7) QT_TRID_NOOP()

另请参阅

Qt 国际化

PySide.QtScript.QScriptEngine. isEvaluating ( )
返回类型: PySide.QtCore.bool

Returns true if this engine is currently evaluating a script, otherwise returns false.

PySide.QtScript.QScriptEngine. newActivationObject ( )
返回类型: PySide.QtScript.QScriptValue
PySide.QtScript.QScriptEngine. newArray ( [ length=0 ] )
参数: length PySide.QtCore.uint
返回类型: PySide.QtScript.QScriptValue

创建 QtScript object of class Array with the given length .

PySide.QtScript.QScriptEngine. newDate ( value )
参数: value PySide.QtCore.double
返回类型: PySide.QtScript.QScriptValue
PySide.QtScript.QScriptEngine. newDate ( value )
参数: value PySide.QtCore.QDateTime
返回类型: PySide.QtScript.QScriptValue

创建 QtScript object of class Date from the given value .

PySide.QtScript.QScriptEngine. newObject ( )
返回类型: PySide.QtScript.QScriptValue

创建 QtScript object of class Object.

The prototype of the created object will be the Object prototype object.

PySide.QtScript.QScriptEngine. newObject ( scriptClass [ , data=QScriptValue() ] )
参数:
返回类型:

PySide.QtScript.QScriptValue

这是重载函数。

创建 QtScript Object of the given class, scriptClass .

The prototype of the created object will be the Object prototype object.

data , if specified, is set as the internal data of the new object (using QScriptValue.setData() ).

PySide.QtScript.QScriptEngine. newQMetaObject ( metaObject [ , ctor=QScriptValue() ] )
参数:
返回类型:

PySide.QtScript.QScriptValue

创建 QtScript object that represents a PySide.QtCore.QObject class, using the the given metaObject and constructor ctor .

Enums of metaObject (declared with Q_ENUMS) are available as properties of the created PySide.QtScript.QScriptValue . When the class is called as a function, ctor will be called to create a new instance of the class.

范例:

def mySpecialQObjectConstructor(context, engine):
    parent = context.argument(0).toQObject()
    object =  QObject(parent)
    return engine.QObject(object, QScriptEngine.ScriptOwnership)
...
ctor = engine.Function(mySpecialQObjectConstructor)
metaObject = engine.QMetaObject(QObject.staticMetaObject, ctor)
engine.globalObject().setProperty("QObject", metaObject)
result = engine.evaluate(" QObject()")
												

另请参阅

PySide.QtScript.QScriptEngine.newQObject() PySide.QtScript.QScriptEngine.scriptValueFromQMetaObject()

PySide.QtScript.QScriptEngine. newQObject ( object [ , ownership=QtOwnership [ , options=0 ] ] )
参数:
返回类型:

PySide.QtScript.QScriptValue

创建 QtScript object that wraps the given PySide.QtCore.QObject object ,使用给定 ownership . The given options control various aspects of the interaction with the resulting script object.

Signals and slots, properties and children of object are available as properties of the created PySide.QtScript.QScriptValue . For more information, see the QtScript 文档编制。

object is a null pointer, this function returns PySide.QtScript.QScriptEngine.nullValue() .

If a default prototype has been registered for the object ‘s class (or its superclass, recursively), the prototype of the new script object will be set to be that default prototype.

若给定 object is deleted outside of QtScript ‘s control, any attempt to access the deleted PySide.QtCore.QObject ‘s members through the QtScript wrapper object (either by script code or C++) will result in a script exception.

PySide.QtScript.QScriptEngine. newQObject ( scriptObject , qtObject [ , ownership=QtOwnership [ , options=0 ] ] )
参数:
返回类型:

PySide.QtScript.QScriptValue

这是重载函数。

Initializes the given scriptObject to hold the given qtObject , and returns the scriptObject .

This function enables you to “promote” a plain Qt Script object (created by the PySide.QtScript.QScriptEngine.newObject() function) to a PySide.QtCore.QObject proxy, or to replace the PySide.QtCore.QObject contained inside an object previously created by the PySide.QtScript.QScriptEngine.newQObject() 函数。

The prototype() of the scriptObject will remain unchanged.

scriptObject is not an object, this function behaves like the normal PySide.QtScript.QScriptEngine.newQObject() , i.e. it creates a new script object and returns it.

This function is useful when you want to provide a script constructor for a PySide.QtCore.QObject -based class. If your constructor is invoked in a new expression ( QScriptContext.isCalledAsConstructor() returns true), you can pass QScriptContext.thisObject() (the default constructed script object) to this function to initialize the new object.

PySide.QtScript.QScriptEngine. newRegExp ( pattern , flags )
参数:
  • pattern – unicode
  • flags – unicode
返回类型:

PySide.QtScript.QScriptValue

创建 QtScript object of class RegExp with the given pattern and flags .

The legal flags are ‘g' (global), ‘i' (ignore case), and ‘m' (multiline).

PySide.QtScript.QScriptEngine. newRegExp ( regexp )
参数: regexp PySide.QtCore.QRegExp
返回类型: PySide.QtScript.QScriptValue

创建 QtScript object of class RegExp with the given regexp .

PySide.QtScript.QScriptEngine. newVariant ( value )
参数: value – object
返回类型: PySide.QtScript.QScriptValue

创建 QtScript object holding the given variant value .

If a default prototype has been registered with the meta type id of value , then the prototype of the created object will be that prototype; otherwise, the prototype will be the Object prototype object.

PySide.QtScript.QScriptEngine. newVariant ( object , value )
参数:
返回类型:

PySide.QtScript.QScriptValue

这是重载函数。

Initializes the given Qt Script object to hold the given variant value , and returns the object .

This function enables you to “promote” a plain Qt Script object (created by the PySide.QtScript.QScriptEngine.newObject() function) to a variant, or to replace the variant contained inside an object previously created by the PySide.QtScript.QScriptEngine.newVariant() 函数。

The prototype() of the object will remain unchanged.

object is not an object, this function behaves like the normal PySide.QtScript.QScriptEngine.newVariant() , i.e. it creates a new script object and returns it.

This function is useful when you want to provide a script constructor for a C++ type. If your constructor is invoked in a new expression ( QScriptContext.isCalledAsConstructor() returns true), you can pass QScriptContext.thisObject() (the default constructed script object) to this function to initialize the new object.

PySide.QtScript.QScriptEngine. nullValue ( )
返回类型: PySide.QtScript.QScriptValue

返回 PySide.QtScript.QScriptValue of the primitive type Null.

PySide.QtScript.QScriptEngine. objectById ( id )
参数: id PySide.QtCore.qint64
返回类型: PySide.QtScript.QScriptValue

Returns the object with the given id , or an invalid PySide.QtScript.QScriptValue if there is no object with that id.

PySide.QtScript.QScriptEngine. popContext ( )

Pops the current execution context and restores the previous one. This function must be used in conjunction with PySide.QtScript.QScriptEngine.pushContext() .

PySide.QtScript.QScriptEngine. processEventsInterval ( )
返回类型: PySide.QtCore.int

Returns the interval in milliseconds between calls to QCoreApplication.processEvents() while the interpreter is running.

PySide.QtScript.QScriptEngine. pushContext ( )
返回类型: PySide.QtScript.QScriptContext

Enters a new execution context and returns the associated PySide.QtScript.QScriptContext 对象。

Once you are done with the context, you should call PySide.QtScript.QScriptEngine.popContext() to restore the old context.

默认情况下, this' object of the new context is the Global Object. The context's :meth:`PySide.QtScript.QScriptContext.callee () will be invalid.

This function is useful when you want to evaluate script code as if it were the body of a function. You can use the context's PySide.QtScript.QScriptContext.activationObject() () to initialize local variables that will be available to scripts. Example:

engine = QScriptEngine()
context = engine.pushContext()
context.activationObject().setProperty("myArg", 123)
engine.evaluate("var tmp = myArg + 42")
...
engine.popContext()
												

In the above example, the new variable “tmp” defined in the script will be local to the context; in other words, the script doesn't have any effect on the global environment.

Returns 0 in case of stack overflow

PySide.QtScript.QScriptEngine. reportAdditionalMemoryCost ( size )
参数: size PySide.QtCore.int

Reports an additional memory cost of the given size , measured in bytes, to the garbage collector.

This function can be called to indicate that a Qt Script object has memory associated with it that isn't managed by Qt Script itself. Reporting the additional cost makes it more likely that the garbage collector will be triggered.

Note that if the additional memory is shared with objects outside the scripting environment, the cost should not be reported, since collecting the Qt Script object would not cause the memory to be freed anyway.

Negative size values are ignored, i.e. this function can't be used to report that the additional memory has been deallocated.

PySide.QtScript.QScriptEngine. setAgent ( agent )
参数: agent PySide.QtScript.QScriptEngineAgent

Installs the given agent on this engine. The agent will be notified of various events pertaining to script execution. This is useful when you want to find out exactly what the engine is doing, e.g. when PySide.QtScript.QScriptEngine.evaluate() is called. The agent interface is the basis of tools like debuggers and profilers.

The engine maintains ownership of the agent .

Calling this function will replace the existing agent, if any.

PySide.QtScript.QScriptEngine. setDefaultPrototype ( metaTypeId , prototype )
参数:

Sets the default prototype of the C++ type identified by the given metaTypeId to prototype .

The default prototype provides a script interface for values of type metaTypeId when a value of that type is accessed from script code. Whenever the script engine (implicitly or explicitly) creates a PySide.QtScript.QScriptValue from a value of type metaTypeId , the default prototype will be set as the PySide.QtScript.QScriptValue ‘s prototype.

prototype object itself may be constructed using one of two principal techniques; the simplest is to subclass PySide.QtScript.QScriptable , which enables you to define the scripting API of the type through PySide.QtCore.QObject properties and slots. Another possibility is to create a script object by calling PySide.QtScript.QScriptEngine.newObject() , and populate the object with the desired properties (e.g. native functions wrapped with newFunction() ).

另请参阅

PySide.QtScript.QScriptEngine.defaultPrototype() qScriptRegisterMetaType() PySide.QtScript.QScriptable Default Prototypes Example

PySide.QtScript.QScriptEngine. setGlobalObject ( object )
参数: object PySide.QtScript.QScriptValue

Sets this engine's Global Object to be the given object 。若 object is not a valid script object, this function does nothing.

When setting a custom global object, you may want to use PySide.QtScript.QScriptValueIterator to copy the properties of the standard Global Object; alternatively, you can set the internal prototype of your custom object to be the original Global Object.

PySide.QtScript.QScriptEngine. setProcessEventsInterval ( interval )
参数: interval PySide.QtCore.int

Sets the interval between calls to QCoreApplication::processEvents to interval 毫秒。

While the interpreter is running, all event processing is by default blocked. This means for instance that the gui will not be updated and timers will not be fired. To allow event processing during interpreter execution one can specify the processing interval to be a positive value, indicating the number of milliseconds between each time QCoreApplication.processEvents() 被调用。

The default value is -1, which disables event processing during interpreter execution.

可以使用 QCoreApplication.postEvent() to post an event that performs custom processing at the next interval. For example, you could keep track of the total running time of the script and call PySide.QtScript.QScriptEngine.abortEvaluation() when you detect that the script has been running for a long time without completing.

PySide.QtScript.QScriptEngine. signalHandlerException ( exception )
参数: exception PySide.QtScript.QScriptValue
PySide.QtScript.QScriptEngine. toObject ( value )
参数: value PySide.QtScript.QScriptValue
返回类型: PySide.QtScript.QScriptValue

转换给定 value to an object, if such a conversion is possible; otherwise returns an invalid PySide.QtScript.QScriptValue . The conversion is performed according to the following table:

Input Type 结果
Undefined An invalid PySide.QtScript.QScriptValue .
Null An invalid PySide.QtScript.QScriptValue .
布尔 A new Boolean object whose internal value is set to the value of the boolean.
Number A new Number object whose internal value is set to the value of the number.
字符串 A new String object whose internal value is set to the value of the string.
对象 The result is the object itself (no conversion).
PySide.QtScript.QScriptEngine. toStringHandle ( str )
参数: str – unicode
返回类型: PySide.QtScript.QScriptString

Returns a handle that represents the given string, str .

PySide.QtScript.QScriptString can be used to quickly look up properties, and compare property names, of script objects.

PySide.QtScript.QScriptEngine. uncaughtException ( )
返回类型: PySide.QtScript.QScriptValue

Returns the current uncaught exception, or an invalid PySide.QtScript.QScriptValue if there is no uncaught exception.

The exception value is typically an Error object; in that case, you can call toString() on the return value to obtain an error message.

PySide.QtScript.QScriptEngine. uncaughtExceptionBacktrace ( )
返回类型: list of strings

Returns a human-readable backtrace of the last uncaught exception.

It is in the form <function-name>()@<file-name>:<line-number> .

PySide.QtScript.QScriptEngine. uncaughtExceptionLineNumber ( )
返回类型: PySide.QtCore.int

Returns the line number where the last uncaught exception occurred.

Line numbers are 1-based, unless a different base was specified as the second argument to PySide.QtScript.QScriptEngine.evaluate() .

PySide.QtScript.QScriptEngine. undefinedValue ( )
返回类型: PySide.QtScript.QScriptValue

返回 PySide.QtScript.QScriptValue of the primitive type Undefined.