Logo Search packages:      
Sourcecode: qt4-x11 version File versions

QScriptEngine Class Reference

Inheritance diagram for QScriptEngine:

QObject

List of all members.


Detailed Description

The QScriptEngine class provides an environment for evaluating Qt Script code.

Since:
4.3

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

Use evaluate() to evaluate script code.

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

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 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 uncaughtException(). You can obtain a human-readable backtrace of the exception with uncaughtExceptionBacktrace().

    QScriptValue result = myEngine.evaluate(...);
    if (myEngine.hasUncaughtException()) {
        int line = myEngine.uncaughtExceptionLineNumber();
        qDebug() << "uncaught exception at line" << line << ":" << result.toString();
    }

When handling possibly incomplete input, the canEvaluate() function can be used to determine whether code can usefully be passed to evaluate(). This can be useful when implementing tools that allow code to be written incrementally, such as command line interpreters.

Use newObject() to create a standard Qt Script object. You can use the object-specific functionality in QScriptValue to manipulate the script object (e.g. QScriptValue::setProperty()). Use newArray() to create a Qt script array object. Use newDate() to create a {Date} object, and newRegExp() to create a {RegExp} object. Use newVariant() to wrap a QVariant.

Use newQObject() to wrap a QObject (or subclass) pointer, and newQMetaObject() to wrap a QMetaObject. When wrapping a QObject pointer with newQObject(), properties, children and signals and slots of the QObject will then become available to script code as properties of the created Qt Script object. No binding code is needed because it is done dynamically using the Qt meta object system. See the {QtScript} documentation for more information.

Use newFunction() to wrap native (C++) functions, including constructors for your own custom types.

Use importExtension() to import plugin-based extensions into the engine.

Use globalObject() to access the unique {Global Object} associated with the script engine. Properties of the Global Object are accessible from any script code. Typically, you set properties in the engine's Global Object to make your own extensions available to scripts. Here is an example of how to expose a number value through the Global Object:

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

In addition to exposing plain data, you can also write C++ functions that 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:

    QScriptValue myAdd(QScriptContext *context, QScriptEngine *engine)
    {
       QScriptValue a = context->argument(0);
       QScriptValue b = context->argument(1);
       return QScriptValue(engine, a.toNumber() + b.toNumber());
    }

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

    QScriptValue fun = myEngine.newFunction(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:

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

You can define shared script functionality for a custom C++ type by creating your own default prototype object and setting it with setDefaultPrototype(); see also QScriptable.

Use fromScriptValue() to cast from a QScriptValue to another type, and toScriptValue() to create a QScriptValue from another value. You can specify how the conversion of C++ types is to be performed with qScriptRegisterMetaType() and qScriptRegisterSequenceMetaType().

See also:
QScriptValue, QScriptContext

Definition at line 91 of file qscriptengine.h.


Public Types

typedef void(* DemarshalFunction )(const QScriptValue &, void *)
typedef QScriptValue(* FunctionSignature )(QScriptContext *, QScriptEngine *)
typedef QScriptValue(* MarshalFunction )(QScriptEngine *, const void *)
enum  QObjectWrapOption { ExcludeChildObjects = 0x0001, ExcludeSuperClassMethods = 0x0002, ExcludeSuperClassProperties = 0x0004, AutoCreateDynamicProperties = 0x0100 }
enum  ValueOwnership { QtOwnership, ScriptOwnership, AutoOwnership }

Public Slots

void deleteLater ()

Signals

void destroyed (QObject *=0)

Public Member Functions

bool blockSignals (bool b)
bool canEvaluate (const QString &program) const
const QObjectListchildren () const
void collectGarbage ()
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
QScriptContextcurrentContext () const
QScriptValue defaultPrototype (int metaTypeId) const
bool disconnect (const QObject *receiver, const char *member=0)
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
void dumpObjectInfo ()
void dumpObjectTree ()
QList< QByteArraydynamicPropertyNames () const
QScriptValue evaluate (const QString &program, const QString &fileName=QString(), int lineNumber=1)
virtual bool event (QEvent *)
virtual bool eventFilter (QObject *, QEvent *)
template<typename T>
findChild (const QString &name=QString()) const
template<typename T>
QList< T > findChildren (const QRegExp &re) const
template<typename T>
QList< T > findChildren (const QString &name=QString()) const
template<typename T>
fromScriptValue (const QScriptValue &value)
QScriptValue globalObject () const
bool hasUncaughtException () const
QScriptValue importExtension (const QString &extension)
bool inherits (const char *classname) const
void installEventFilter (QObject *)
bool isWidgetType () const
void killTimer (int id)
void moveToThread (QThread *thread)
QScriptValue newActivationObject ()
QScriptValue newArray (uint length=0)
QScriptValue newDate (const QDateTime &value)
QScriptValue newDate (qsreal value)
QScriptValue newFunction (FunctionSignature signature, const QScriptValue &prototype, int length=0)
QScriptValue newFunction (FunctionSignature signature, int length=0)
QScriptValue newObject ()
QScriptValue newQMetaObject (const QMetaObject *metaObject, const QScriptValue &ctor=QScriptValue())
QScriptValue newQObject (QObject *object, ValueOwnership ownership=QtOwnership, const QObjectWrapOptions &options=0)
QScriptValue newRegExp (const QString &pattern, const QString &flags)
QScriptValue newRegExp (const QRegExp &regexp)
QScriptValue newVariant (const QVariant &value)
QScriptValue nullValue ()
QString objectName () const
QObjectparent () const
void popContext ()
int processEventsInterval () const
QVariant property (const char *name) const
QScriptContextpushContext ()
 QScriptEngine (QObject *parent)
 QScriptEngine ()
void removeEventFilter (QObject *)
template<class T>
QScriptValue scriptValueFromQMetaObject ()
void setDefaultPrototype (int metaTypeId, const QScriptValue &prototype)
void setObjectName (const QString &name)
void setParent (QObject *)
void setProcessEventsInterval (int interval)
bool setProperty (const char *name, const QVariant &value)
void setUserData (uint id, QObjectUserData *data)
bool signalsBlocked () const
int startTimer (int interval)
QThreadthread () const
template<typename T>
QScriptValue toScriptValue (const T &value)
QScriptValue uncaughtException () const
QStringList uncaughtExceptionBacktrace () const
int uncaughtExceptionLineNumber () const
QScriptValue undefinedValue ()
QObjectUserData * userData (uint id) const
virtual ~QScriptEngine ()

Static Public Member Functions

static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
static uint registerUserData ()

Protected Member Functions

virtual void childEvent (QChildEvent *)
virtual void connectNotify (const char *signal)
virtual void customEvent (QEvent *)
virtual void disconnectNotify (const char *signal)
 QScriptEngine (QScriptEnginePrivate &dd, QObject *parent=0)
int receivers (const char *signal) const
QObjectsender () const
virtual void timerEvent (QTimerEvent *)

Protected Attributes

QObjectData * d_ptr

Static Protected Attributes

static const QMetaObject staticQtMetaObject

Properties

QString objectName
 the name of this object

Private Member Functions

bool convert (const QScriptValue &value, int type, void *ptr)
QScriptValue create (int type, const void *ptr)
void registerCustomType (int type, MarshalFunction mf, DemarshalFunction df, const QScriptValue &prototype)

Friends

class QApplication
class QApplicationPrivate
class QCoreApplication
class QCoreApplicationPrivate
struct QMetaObject
void qScriptRegisterMetaType_helper (QScriptEngine *, int, MarshalFunction, DemarshalFunction, const QScriptValue &)
bool qscriptvalue_cast_helper (const QScriptValue &, int, void *)
QScriptValue qScriptValueFromValue_helper (QScriptEngine *, int, const void *)
class QThreadData
class QWidget

Related Functions

(Note that these are not member functions.)

 FunctionSignature
qFindChild (const QObject *obj, const QString &name=QString(), T dummy=0)
qFindChild (const QObject *obj, const QString &name)
QList< T > qFindChildren (const QObject *obj, const QString &name=QString(), T dummy=0)
QList< T > qFindChildren (const QObject *obj, const QRegExp &regExp)
QList< T > qFindChildren (const QObject *obj, const QString &name)
T * qobject_cast (QObject *object)
 QObjectList
int qScriptRegisterMetaType (QScriptEngine *engine, QScriptValue(*toScriptValue)(QScriptEngine *, const T &t), void(*fromScriptValue)(const QScriptValue &, T &t), const QScriptValue &prototype=QScriptValue())
int qScriptRegisterSequenceMetaType (QScriptEngine *engine, const QScriptValue &prototype=QScriptValue())
QScriptValue qScriptValueFromQMetaObject (QScriptEngine *engine)
QScriptValue qScriptValueFromSequence (QScriptEngine *engine, const Container &container)
QScriptValue qScriptValueFromValue (QScriptEngine *engine, const T &value)
void qScriptValueToSequence (const QScriptValue &value, Container &container)
qScriptValueToValue (const QScriptValue &value)
void * qt_find_obj_child (QObject *parent, const char *type, const QString &name)

The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index