- Qt 5.15
- Qt Script
- QScriptEngine Proxy Page
- Qt 5.15.18 Reference Documentation

# QScriptEngine Proxy Page

## Functions

Q_DECL_EXPORT | qScriptConnect(QObject *sender, const char *signal, const QScriptValue &receiver, const QScriptValue &function)
Q_DECL_EXPORT | qScriptDisconnect(QObject *sender, const char *signal, const QScriptValue &receiver, const QScriptValue &function)
int | qScriptRegisterMetaType(QScriptEngine *eng, QScriptValue (*)(QScriptEngine *, const T &) toScriptValue, void (*)(const QScriptValue &, T &) fromScriptValue, const QScriptValue &prototype = QScriptValue(), T * = 0)
int | qScriptRegisterSequenceMetaType(QScriptEngine *engine, const QScriptValue &prototype = QScriptValue(), T * = 0)
QScriptValue | qScriptValueFromSequence(QScriptEngine *engine, const Container &container)
void | qScriptValueToSequence(const QScriptValue &value, Container &container)

## Macros

| Q_SCRIPT_DECLARE_QMETAOBJECT(QMetaObject, ArgType)

## Function Documentation

### Q_DECL_EXPORT qScriptConnect(QObject *sender, const char *signal, const QScriptValue &receiver, const QScriptValue &function)

Creates a connection from the signal in the sender to the given function. If receiver is an object, it will act as the `this' object when the signal handler function is invoked. Returns true if the connection succeeds; otherwise returns false.

This function was introduced in Qt 4.4.

See also qScriptDisconnect() and QScriptEngine::signalHandlerException().

### Q_DECL_EXPORT qScriptDisconnect(QObject *sender, const char *signal, const QScriptValue &receiver, const QScriptValue &function)

Disconnects the signal in the sender from the given (receiver, function) pair. Returns true if the connection is successfully broken; otherwise returns false.

This function was introduced in Qt 4.4.

See also qScriptConnect().

### template <typename T> int qScriptRegisterMetaType(QScriptEngine *eng, QScriptValue (*)(QScriptEngine *, const T &) toScriptValue, void (*)(const QScriptValue &, T &) fromScriptValue, const QScriptValue &prototype = QScriptValue(), T * = 0)

Registers the type `T` in the given eng. toScriptValue must be a function that will convert from a value of type `T` to a QScriptValue, and fromScriptValue a function that does the opposite. prototype, if valid, is the prototype that's set on QScriptValues returned by toScriptValue.

Returns the internal ID used by QMetaType.

You only need to call this function if you want to provide custom conversion of values of type `T`, i.e. if the default QVariant-based representation and conversion is not appropriate. (Note that custom QObject-derived types also fall in this category; e.g. for a QObject-derived class called MyObject, you probably want to define conversion functions for MyObject* that utilize QScriptEngine::newQObject() and QScriptValue::toQObject().)

If you only want to define a common script interface for values of type `T`, and don't care how those values are represented (i.e. storing them in QVariants is fine), use setDefaultPrototype() instead; this will minimize conversion costs.

You need to declare the custom type first with Q_DECLARE_METATYPE().

After a type has been registered, you can convert from a QScriptValue to that type using fromScriptValue(), and create a QScriptValue from a value of that type using toScriptValue(). The engine will take care of calling the proper conversion function when calling C++ slots, and when getting or setting a C++ property; i.e. the custom type may be used seamlessly on both the C++ side and the script side.

The following is an example of how to use this function. We will specify custom conversion of our type `MyStruct`. Here's the C++ type:

     struct MyStruct {
         int x;
         int y;
     };

We must declare it so that the type will be known to QMetaType:

     Q_DECLARE_METATYPE(MyStruct)

Next, the `MyStruct` conversion functions. We represent the `MyStruct` value as a script object and just copy the properties:

     QScriptValue toScriptValue(QScriptEngine *engine, const MyStruct &s)
     {
       QScriptValue obj = engine->newObject();
       obj.setProperty("x", s.x);
       obj.setProperty("y", s.y);
       return obj;
     }

     void fromScriptValue(const QScriptValue &obj, MyStruct &s)
     {
       s.x = obj.property("x").toInt32();
       s.y = obj.property("y").toInt32();
     }

Now we can register `MyStruct` with the engine:

     qScriptRegisterMetaType(engine, toScriptValue, fromScriptValue);

Working with `MyStruct` values is now easy:

     MyStruct s = qscriptvalue_cast<MyStruct>(context->argument(0));
     ...
     MyStruct s2;
     s2.x = s.x + 10;
     s2.y = s.y + 20;
     QScriptValue v = engine->toScriptValue(s2);

If you want to be able to construct values of your custom type from script code, you have to register a constructor function for the type. For example:

     QScriptValue createMyStruct(QScriptContext *, QScriptEngine *engine)
     {
         MyStruct s;
         s.x = 123;
         s.y = 456;
         return engine->toScriptValue(s);
     }
     ...
     QScriptValue ctor = engine.newFunction(createMyStruct);
     engine.globalObject().setProperty("MyStruct", ctor);

See also qScriptRegisterSequenceMetaType() and qRegisterMetaType().

### template <typename T> int qScriptRegisterSequenceMetaType(QScriptEngine *engine, const QScriptValue &prototype = QScriptValue(), T * = 0)

Registers the sequence type `T` in the given engine. This function provides conversion functions that convert between `T` and Qt Script `Array` objects. `T` must provide a const_iterator class and begin(), end() and push_back() functions. If prototype is valid, it will be set as the prototype of `Array` objects due to conversion from `T`; otherwise, the standard `Array` prototype will be used.

Returns the internal ID used by QMetaType.

You need to declare the container type first with Q_DECLARE_METATYPE(). If the element type isn't a standard Qt/C++ type, it must be declared using Q_DECLARE_METATYPE() as well. Example:

     Q_DECLARE_METATYPE(QVector<int>)

     ...

     qScriptRegisterSequenceMetaType<QVector<int> >(engine);
     ...
     QVector<int> v = qscriptvalue_cast<QVector<int> >(engine->evaluate("[5, 1, 3, 2]"));
     std::sort(v.begin(), v.end());
     QScriptValue a = engine->toScriptValue(v);
     qDebug() << a.toString(); // outputs "[1, 2, 3, 5]"

See also qScriptRegisterMetaType().

### template <typename Container> QScriptValue qScriptValueFromSequence(QScriptEngine *engine, const Container &container)

Creates an array in the form of a QScriptValue using the given engine with the given container of template type `Container`.

The `Container` type must provide a `const_iterator` class to enable the contents of the container to be copied into the array.

Additionally, the type of each element in the sequence should be suitable for conversion to a QScriptValue. See Conversion Between Qt Script and C++ Types for more information about the restrictions on types that can be used with QScriptValue.

This function was introduced in Qt 4.3.

See also QScriptEngine::fromScriptValue().

### template <typename Container> void qScriptValueToSequence(const QScriptValue &value, Container &container)

Copies the elements in the sequence specified by value to the given container of template type `Container`.

The value used is typically an array, but any container can be copied as long as it provides a `length` property describing how many elements it contains.

Additionally, the type of each element in the sequence must be suitable for conversion to a C++ type from a QScriptValue. See Conversion Between Qt Script and C++ Types for more information about the restrictions on types that can be used with QScriptValue.

This function was introduced in Qt 4.3.

See also qscriptvalue_cast().

## Macro Documentation

### Q_SCRIPT_DECLARE_QMETAOBJECT(QMetaObject, ArgType)

Declares the given QMetaObject. Used in combination with QScriptEngine::scriptValueFromQMetaObject() to make enums and instantiation of QMetaObject available to script code. The constructor generated by this macro takes a single argument of type ArgType; typically the argument is the parent type of the new instance, in which case ArgType is `QWidget*` or `QObject*`. Objects created by the constructor will have QScriptEngine::AutoOwnership ownership.

This function was introduced in Qt 4.3.