Qt 4.8
Public Types | Signals | Public Functions | Private Functions | Properties | Friends | List of all members
QAxScript Class Reference

The QAxScript class provides a wrapper around script code. More...

#include <qaxscript.h>

Inheritance diagram for QAxScript:
QObject

Public Types

enum  FunctionFlags { FunctionNames = 0, FunctionSignatures }
 This FunctionFlags enum describes formatting for function introspection. More...
 

Signals

void entered ()
 This signal is emitted when a script engine has started executing code. More...
 
void error (int code, const QString &description, int sourcePosition, const QString &sourceText)
 This signal is emitted when an execution error occurred while running a script. More...
 
void finished ()
 This signal is emitted when a script engine has finished executing code. More...
 
void finished (const QVariant &result)
 QVariant if the script has no return value. More...
 
void finished (int code, const QString &source, const QString &description, const QString &help)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.code, source, description and help contain exception information when the script terminated. More...
 
void stateChanged (int state)
 This signal is emitted when a script engine changes state. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 

Public Functions

QVariant call (const QString &function, const QVariant &v1=QVariant(), const QVariant &v2=QVariant(), const QVariant &v3=QVariant(), const QVariant &v4=QVariant(), const QVariant &v5=QVariant(), const QVariant &v6=QVariant(), const QVariant &v7=QVariant(), const QVariant &v8=QVariant())
 Calls function, passing the parameters var1, var1, var2, var3, var4, var5, var6, var7 and var8 as arguments and returns the value returned by the function, or an invalid QVariant if the function does not return a value or when the function call failed. More...
 
QVariant call (const QString &function, QList< QVariant > &arguments)
 Calls function passing arguments as parameters, and returns the result. More...
 
QStringList functions (FunctionFlags=FunctionNames) const
 Returns a list of all the functions in this script if the respective script engine supports introspection; otherwise returns an empty list. More...
 
bool load (const QString &code, const QString &language=QString())
 Loads the script source code written in language language into the script engine. More...
 
 QAxScript (const QString &name, QAxScriptManager *manager)
 Constructs a QAxScript object called name and registers it with the QAxScriptManager manager. More...
 
QString scriptCode () const
 Returns the script's code, or the null-string if no code has been loaded yet. More...
 
QAxScriptEnginescriptEngine () const
 Returns a pointer to the script engine. More...
 
QString scriptName () const
 Returns the name of the script. More...
 
 ~QAxScript ()
 Destroys the object, releasing all allocated resources. More...
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Private Functions

QAxBasefindObject (const QString &name)
 Returns the object name registered with the manager. More...
 
void updateObjects ()
 Registers all objects in the manager with the script engine. More...
 

Properties

QString script_code
 
QAxScriptEnginescript_engine
 
QAxScriptManagerscript_manager
 
QString script_name
 
QAxScriptSitescript_site
 

Friends

class QAxScriptEngine
 
class QAxScriptSite
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

The QAxScript class provides a wrapper around script code.

Attention
Module: QAxContainer

Every instance of the QAxScript class represents a piece of scripting code in a particular scripting language. The code is loaded into the script engine using load(). Functions declared in the code can be called using call().

The script provides scriptEngine() provides feedback to the application through signals. The most important signal is the error() signal. Direct access to the QAxScriptEngine is provided through the scriptEngine() function.

Warning
This class is not available with the bcc5.5 and MingW compilers.
See also
QAxScriptEngine, QAxScriptManager, QAxBase, {ActiveQt Framework}

Definition at line 100 of file qaxscript.h.

Enumerations

◆ FunctionFlags

This FunctionFlags enum describes formatting for function introspection.

  • FunctionNames Only function names are returned.
  • FunctionSignatures Returns the functions with signatures.
Enumerator
FunctionNames 
FunctionSignatures 

Definition at line 105 of file qaxscript.h.

Constructors and Destructors

◆ QAxScript()

QAxScript::QAxScript ( const QString name,
QAxScriptManager manager 
)

Constructs a QAxScript object called name and registers it with the QAxScriptManager manager.

This is usually done by the QAxScriptManager class when loading a script.

A script should always have a name. A manager is necessary to allow the script code to reference objects in the application. The manager takes ownership of the object.

Definition at line 676 of file qaxscript.cpp.

677 : QObject(manager), script_name(name), script_manager(manager),
678 script_engine(0)
679 {
680  if (manager) {
681  manager->d->scriptDict.insert(name, this);
682  connect(this, SIGNAL(error(int,QString,int,QString)),
683  manager, SLOT(scriptError(int,QString,int,QString)));
684  }
685 
686 #ifndef QT_NO_QAXSCRIPT
687  script_site = new QAxScriptSite(this);
688 #else
689  script_site = 0;
690 #endif
691 }
#define SLOT(a)
Definition: qobjectdefs.h:226
The QString class provides a Unicode character string.
Definition: qstring.h:83
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
QAxScriptManager * script_manager
Definition: qaxscript.h:148
#define SIGNAL(a)
Definition: qobjectdefs.h:227
void error(int code, const QString &description, int sourcePosition, const QString &sourceText)
This signal is emitted when an execution error occurred while running a script.
QAxScriptManagerPrivate * d
Definition: qaxscript.h:193
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
QString script_name
Definition: qaxscript.h:146
QAxScriptSite * script_site
Definition: qaxscript.h:150
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753
QAxScriptEngine * script_engine
Definition: qaxscript.h:149
friend class QAxScriptSite
Definition: qaxscript.h:140
QHash< QString, QAxScript * > scriptDict
Definition: qaxscript.cpp:74

◆ ~QAxScript()

QAxScript::~QAxScript ( )

Destroys the object, releasing all allocated resources.

Definition at line 696 of file qaxscript.cpp.

697 {
698  delete script_engine;
699  script_engine = 0;
700 
701 #ifndef QT_NO_QAXSCRIPT
702  script_site->Release();
703 #endif
704 }
QAxScriptSite * script_site
Definition: qaxscript.h:150
QAxScriptEngine * script_engine
Definition: qaxscript.h:149
ULONG WINAPI Release()
Definition: qaxscript.cpp:138

Functions

◆ call() [1/2]

QVariant QAxScript::call ( const QString function,
const QVariant var1 = QVariant(),
const QVariant var2 = QVariant(),
const QVariant var3 = QVariant(),
const QVariant var4 = QVariant(),
const QVariant var5 = QVariant(),
const QVariant var6 = QVariant(),
const QVariant var7 = QVariant(),
const QVariant var8 = QVariant() 
)

Calls function, passing the parameters var1, var1, var2, var3, var4, var5, var6, var7 and var8 as arguments and returns the value returned by the function, or an invalid QVariant if the function does not return a value or when the function call failed.

See QAxScriptManager::call() for more information about how to call script functions.

Definition at line 793 of file qaxscript.cpp.

Referenced by QAxScriptManager::call().

801 {
802  if (!script_engine)
803  return QVariant();
804 
805  return script_engine->dynamicCall(function.toLatin1(), var1, var2, var3, var4, var5, var6, var7, var8);
806 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QVariant dynamicCall(const char *name, const QVariant &v1=QVariant(), const QVariant &v2=QVariant(), const QVariant &v3=QVariant(), const QVariant &v4=QVariant(), const QVariant &v5=QVariant(), const QVariant &v6=QVariant(), const QVariant &v7=QVariant(), const QVariant &v8=QVariant())
Calls the COM object&#39;s method function, passing the parameters var1, var1, var2, var3, var4, var5, var6, var7 and var8, and returns the value returned by the method, or an invalid QVariant if the method does not return a value or when the function call failed.
Definition: qaxbase.cpp:4046
QAxScriptEngine * script_engine
Definition: qaxscript.h:149

◆ call() [2/2]

QVariant QAxScript::call ( const QString function,
QList< QVariant > &  arguments 
)

Calls function passing arguments as parameters, and returns the result.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns when the script's execution has finished.

See QAxScriptManager::call() for more information about how to call script functions.

Definition at line 820 of file qaxscript.cpp.

821 {
822  if (!script_engine)
823  return QVariant();
824 
825  return script_engine->dynamicCall(function.toLatin1(), arguments);
826 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QVariant dynamicCall(const char *name, const QVariant &v1=QVariant(), const QVariant &v2=QVariant(), const QVariant &v3=QVariant(), const QVariant &v4=QVariant(), const QVariant &v5=QVariant(), const QVariant &v6=QVariant(), const QVariant &v7=QVariant(), const QVariant &v8=QVariant())
Calls the COM object&#39;s method function, passing the parameters var1, var1, var2, var3, var4, var5, var6, var7 and var8, and returns the value returned by the method, or an invalid QVariant if the method does not return a value or when the function call failed.
Definition: qaxbase.cpp:4046
QAxScriptEngine * script_engine
Definition: qaxscript.h:149

◆ entered

void QAxScript::entered ( )
signal

This signal is emitted when a script engine has started executing code.

Referenced by QAxScriptSite::OnEnterScript().

◆ error

void QAxScript::error ( int  code,
const QString description,
int  sourcePosition,
const QString sourceText 
)
signal

This signal is emitted when an execution error occurred while running a script.

code, description, sourcePosition and sourceText contain information about the execution error.

Referenced by QAxScriptSite::OnScriptError(), and QAxScript().

◆ findObject()

QAxBase * QAxScript::findObject ( const QString name)
private

Returns the object name registered with the manager.

Warning
This function is not part of the public interface.

Definition at line 848 of file qaxscript.cpp.

Referenced by QAxScriptSite::GetItemInfo().

849 {
850  if (!script_manager)
851  return 0;
852 
853  return script_manager->d->objectDict.value(name);
854 }
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
QAxScriptManager * script_manager
Definition: qaxscript.h:148
QAxScriptManagerPrivate * d
Definition: qaxscript.h:193
QHash< QString, QAxBase * > objectDict
Definition: qaxscript.cpp:75

◆ finished [1/3]

void QAxScript::finished ( )
signal

This signal is emitted when a script engine has finished executing code.

Referenced by QAxScriptSite::OnLeaveScript(), and QAxScriptSite::OnScriptTerminate().

◆ finished [2/3]

void QAxScript::finished ( const QVariant result)
signal

QVariant if the script has no return value.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

result contains the script's result. This will be an invalid

◆ finished [3/3]

void QAxScript::finished ( int  code,
const QString source,
const QString description,
const QString help 
)
signal

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.code, source, description and help contain exception information when the script terminated.

◆ functions()

QStringList QAxScript::functions ( FunctionFlags  flags = FunctionNames) const

Returns a list of all the functions in this script if the respective script engine supports introspection; otherwise returns an empty list.

The functions are either provided with full prototypes or only as names, depending on the value of flags.

See also
QAxScriptEngine::hasIntrospection()

Definition at line 761 of file qaxscript.cpp.

Referenced by QAxScriptManager::functions(), and QAxScriptManager::scriptForFunction().

762 {
764 
765  const QMetaObject *mo = script_engine->metaObject();
766  for (int i = mo->methodOffset(); i < mo->methodCount(); ++i) {
767  const QMetaMethod slot(mo->method(i));
768  if (slot.methodType() != QMetaMethod::Slot || slot.access() != QMetaMethod::Public)
769  continue;
770  QString slotname = QString::fromLatin1(slot.signature());
771  if (slotname.contains(QLatin1Char('_')))
772  continue;
773 
774  if (flags == FunctionSignatures)
775  functions << slotname;
776  else
777  functions << slotname.left(slotname.indexOf(QLatin1Char('(')));
778  }
779 
780  return functions;
781 }
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
The QMetaObject class contains meta-information about Qt objects.
Definition: qobjectdefs.h:304
The QString class provides a Unicode character string.
Definition: qstring.h:83
const QMetaObject * metaObject() const
Definition: qaxobject.cpp:131
int methodOffset() const
Returns the method offset for this class; i.e.
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
QAxScriptEngine * script_engine
Definition: qaxscript.h:149
QMetaMethod method(int index) const
Returns the meta-data for the method with the given index.
int methodCount() const
Returns the number of methods known to the meta-object system in this class, including the number of ...
QStringList functions(FunctionFlags=FunctionNames) const
Returns a list of all the functions in this script if the respective script engine supports introspec...
Definition: qaxscript.cpp:761
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
The QMetaMethod class provides meta-data about a member function.
Definition: qmetaobject.h:56

◆ load()

bool QAxScript::load ( const QString code,
const QString language = QString() 
)

Loads the script source code written in language language into the script engine.

Returns true if code was successfully entered into the script engine; otherwise returns false.

If language is empty (the default) it will be determined heuristically. If code contains the string {End Sub} it will be interpreted as VBScript, otherwise as JScript. Additional scripting languages can be registered using QAxScript::registerEngine().

This function can only be called once for each QAxScript object, which is done automatically when using QAxScriptManager::load().

Definition at line 720 of file qaxscript.cpp.

Referenced by QAxScriptManager::load().

721 {
722  if (script_engine || code.isEmpty())
723  return false;
724 
725  script_code = code;
726  QString lang = language;
727  if (lang.isEmpty()) {
728  if (code.contains(QLatin1String("End Sub"), Qt::CaseInsensitive))
729  lang = QLatin1String("VBScript");
730 
732  for (it = engines.begin(); it != engines.end(); ++it) {
733  QAxEngineDescriptor engine = *it;
734  if (engine.code.isEmpty())
735  continue;
736 
737  if (code.contains(engine.code)) {
738  lang = engine.name;
739  break;
740  }
741  }
742  }
743  if (lang.isEmpty())
744  lang = QLatin1String("JScript");
745 
746  script_engine = new QAxScriptEngine(lang, this);
747  // trigger call to initialize
749 
750  return script_engine->isValid();
751 }
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
QString script_code
Definition: qaxscript.h:147
#define it(className, varName)
The QList::const_iterator class provides an STL-style const iterator for QList and QQueue...
Definition: qlist.h:228
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
const QMetaObject * metaObject() const
Definition: qaxobject.cpp:131
bool isValid() const
Returns true if the script engine has been initialized correctly; otherwise returns false...
Definition: qaxscript.h:219
QLocale::Language language
static QList< QAxEngineDescriptor > engines
Definition: qaxscript.cpp:69
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
friend class QAxScriptEngine
Definition: qaxscript.h:141
QAxScriptEngine * script_engine
Definition: qaxscript.h:149

◆ scriptCode()

QString QAxScript::scriptCode ( ) const
inline

Returns the script's code, or the null-string if no code has been loaded yet.

See also
load()

Definition at line 202 of file qaxscript.h.

Referenced by QAxScriptEngine::initialize().

203 {
204  return script_code;
205 }
QString script_code
Definition: qaxscript.h:147

◆ scriptEngine()

QAxScriptEngine * QAxScript::scriptEngine ( ) const
inline

Returns a pointer to the script engine.

You can use the object returned to connect signals to the script functions, or to access the script engine directly.

Definition at line 212 of file qaxscript.h.

Referenced by QAxScriptManager::updateScript().

213 {
214  return script_engine;
215 }
QAxScriptEngine * script_engine
Definition: qaxscript.h:149

◆ scriptName()

QString QAxScript::scriptName ( ) const
inline

Returns the name of the script.

Definition at line 207 of file qaxscript.h.

208 {
209  return script_name;
210 }
QString script_name
Definition: qaxscript.h:146

◆ stateChanged

void QAxScript::stateChanged ( int  state)
signal

This signal is emitted when a script engine changes state.

state can be any value in the QAxScriptEngineState enumeration.

Referenced by QAxScriptSite::OnStateChange().

◆ updateObjects()

void QAxScript::updateObjects ( )
private

Registers all objects in the manager with the script engine.

Warning
This function is not part of the public interface.

Definition at line 834 of file qaxscript.cpp.

Referenced by QAxScriptEngine::initialize().

835 {
836  if (!script_manager)
837  return;
838 
840 }
void updateScript(QAxScript *)
Definition: qaxscript.cpp:1325
QAxScriptManager * script_manager
Definition: qaxscript.h:148

Friends and Related Functions

◆ QAxScriptEngine

friend class QAxScriptEngine
friend

Definition at line 141 of file qaxscript.h.

Referenced by load().

◆ QAxScriptSite

friend class QAxScriptSite
friend

Definition at line 140 of file qaxscript.h.

Referenced by QAxScript().

Properties

◆ script_code

QString QAxScript::script_code
private

Definition at line 147 of file qaxscript.h.

Referenced by load().

◆ script_engine

QAxScriptEngine* QAxScript::script_engine
private

Definition at line 149 of file qaxscript.h.

Referenced by call(), functions(), load(), scriptEngine(), and ~QAxScript().

◆ script_manager

QAxScriptManager* QAxScript::script_manager
private

Definition at line 148 of file qaxscript.h.

Referenced by findObject(), and updateObjects().

◆ script_name

QString QAxScript::script_name
private

Definition at line 146 of file qaxscript.h.

Referenced by scriptName().

◆ script_site

QAxScriptSite* QAxScript::script_site
private

Definition at line 150 of file qaxscript.h.

Referenced by QAxScriptEngine::initialize(), QAxScript(), and ~QAxScript().


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