1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- qscriptable.cpp -->
<head>
<title>QScriptable Class Reference</title>
<link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="57" height="67" border="0" /></td>
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a></td>
</tr></table><h1 class="title">QScriptable Class Reference<br /><span class="small-subtitle">[<a href="http://qt.nokia.com/doc/4.6/qtscript.html">QtScript</a> module]</span>
</h1>
<p>The QScriptable class provides access to the Qt Script environment from Qt C++ member functions. <a href="#details">More...</a></p>
<pre> #include <QScriptable></pre><p><b>This class is not part of the Qt GUI Framework Edition.</b></p>
<p>This class was introduced in qtscriptclassic 4.3.</p>
<ul>
<li><a href="qscriptable-members.html">List of all members, including inherited members</a></li>
</ul>
<hr />
<a name="public-functions"></a>
<h2>Public Functions</h2>
<table class="alignedsummary" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr><td class="memItemLeft" align="right" valign="top">QScriptValue </td><td class="memItemRight" valign="bottom"><b><a href="qscriptable.html#argument">argument</a></b> ( int <i>index</i> ) const</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><b><a href="qscriptable.html#argumentCount">argumentCount</a></b> () const</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">QScriptContext * </td><td class="memItemRight" valign="bottom"><b><a href="qscriptable.html#context">context</a></b> () const</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">QScriptEngine * </td><td class="memItemRight" valign="bottom"><b><a href="qscriptable.html#engine">engine</a></b> () const</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">QScriptValue </td><td class="memItemRight" valign="bottom"><b><a href="qscriptable.html#thisObject">thisObject</a></b> () const</td></tr>
</table>
<a name="details"></a>
<hr />
<h2>Detailed Description</h2>
<p>The QScriptable class provides access to the Qt Script environment from Qt C++ member functions.</p>
<p>With <a href="qscriptengine.html#newQObject">QScriptEngine::newQObject</a>(), you can expose the signals and slots and properties of any <a href="http://qt.nokia.com/doc/4.6/qobject.html">QObject</a> (or subclass) to script code. QScriptable augments this functionality by giving your C++ members access to the Qt Script environment they are invoked in; conceptually, it is similar to <a href="http://qt.nokia.com/doc/4.6/qobject.html#sender">QObject::sender</a>().</p>
<p>By subclassing QScriptable, you get the following functions in your class: <a href="qscriptable.html#thisObject">thisObject</a>(), <a href="qscriptable.html#argumentCount">argumentCount</a>(), <a href="qscriptable.html#argument">argument</a>(), <a href="qscriptable.html#context">context</a>() and <a href="qscriptable.html#engine">engine</a>(). With these functions, you have full access to the Qt Script environment from the slots and property access functions of your class, when they are invoked from script code.</p>
<p>For example, you can throw a Qt Script exception from a slot; manipulate the `this' object associated with the function call; inspect the arguments stored in the <a href="qscriptcontext.html">QScriptContext</a> to know the "real" arguments passed to the function from script code; and call script functions from your slot.</p>
<p>A typical use case of QScriptable is to implement prototype objects for custom C++ types. You define the scriptable interface of your custom type in a QScriptable subclass using properties and slots; then you wrap an instance of your class using <a href="qscriptengine.html#newQObject">QScriptEngine::newQObject</a>(), and finally pass the result to <a href="qscriptengine.html#setDefaultPrototype">QScriptEngine::setDefaultPrototype</a>(). See the <a href="http://qt.nokia.com/doc/4.6/script-defaultprototypes.html">Default Prototypes Example</a> to see how this can be done.</p>
<p>The following is what subclassing QScriptable typically looks like:</p>
<pre> class MyScriptableObject: public QObject,
protected QScriptable
{
Q_OBJECT
...
public slots:
void doSomething();
double doSomethingElse();
}</pre>
<p>The only difference from regular <a href="http://qt.nokia.com/doc/4.6/qobject.html">QObject</a> subclassing is that you also inherit from QScriptable.</p>
<p>In the implementation of your slots, you can then use the functions inherited from QScriptable:</p>
<pre> void MyScriptableObject::doSomething()
{
context()->throwError("Threw an error from a slot");
}
double MyScriptableObject::doSomethingElse()
{
return qscriptvalue_cast<double>(thisObject());
}</pre>
<p>See also <a href="http://qt.nokia.com/doc/4.6/script-defaultprototypes.html">Default Prototypes Example</a> and <a href="qscriptengine.html#newFunction">QScriptEngine::newFunction</a>().</p>
<hr />
<h2>Member Function Documentation</h2>
<h3 class="fn"><a name="argument"></a><a href="qscriptvalue.html">QScriptValue</a> QScriptable::argument ( int <i>index</i> ) const</h3>
<p>Returns the function argument at the given <i>index</i>, or an invalid <a href="qscriptvalue.html">QScriptValue</a> if the Qt function was not invoked from script code.</p>
<p>See also <a href="qscriptable.html#argumentCount">argumentCount</a>().</p>
<h3 class="fn"><a name="argumentCount"></a>int QScriptable::argumentCount () const</h3>
<p>Returns the number of arguments passed to the function in this invocation, or -1 if the Qt function was not invoked from script code.</p>
<p>See also <a href="qscriptable.html#argument">argument</a>().</p>
<h3 class="fn"><a name="context"></a><a href="qscriptcontext.html">QScriptContext</a> * QScriptable::context () const</h3>
<p>Returns a pointer to the <a href="qscriptcontext.html">QScriptContext</a> associated with the current Qt function call, or 0 if the Qt function was not invoked from script code.</p>
<h3 class="fn"><a name="engine"></a><a href="qscriptengine.html">QScriptEngine</a> * QScriptable::engine () const</h3>
<p>Returns a pointer to the <a href="qscriptengine.html">QScriptEngine</a> associated with the current Qt function call, or 0 if the Qt function was not invoked from script code.</p>
<h3 class="fn"><a name="thisObject"></a><a href="qscriptvalue.html">QScriptValue</a> QScriptable::thisObject () const</h3>
<p>Returns the `this' object associated with the current Qt function call, or an invalid <a href="qscriptvalue.html">QScriptValue</a> if the Qt function was not invoked from script code.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%" align="left">Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="40%" align="center"><a href="http://qt.nokia.com/doc/trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt Solutions</div></td>
</tr></table></div></address></body>
</html>
|