<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- defaultprototypes.qdoc --> <title>Qt 4.8: Default Prototypes Example</title> <link rel="stylesheet" type="text/css" href="style/style.css" /> <script src="scripts/jquery.js" type="text/javascript"></script> <script src="scripts/functions.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="style/superfish.css" /> <link rel="stylesheet" type="text/css" href="style/narrow.css" /> <!--[if IE]> <meta name="MSSmartTagsPreventParsing" content="true"> <meta http-equiv="imagetoolbar" content="no"> <![endif]--> <!--[if lt IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie6.css"> <![endif]--> <!--[if IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie7.css"> <![endif]--> <!--[if IE 8]> <link rel="stylesheet" type="text/css" href="style/style_ie8.css"> <![endif]--> <script src="scripts/superfish.js" type="text/javascript"></script> <script src="scripts/narrow.js" type="text/javascript"></script> </head> <body class="" onload="CheckEmptyAndLoadList();"> <div class="header" id="qtdocheader"> <div class="content"> <div id="nav-logo"> <a href="index.html">Home</a></div> <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a> <div id="narrowsearch"></div> <div id="nav-topright"> <ul> <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li> <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li> <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/"> DOC</a></li> <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li> </ul> </div> <div id="shortCut"> <ul> <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li> <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS </a></li> </ul> </div> <ul class="sf-menu" id="narrowmenu"> <li><a href="#">API Lookup</a> <ul> <li><a href="classes.html">Class index</a></li> <li><a href="functions.html">Function index</a></li> <li><a href="modules.html">Modules</a></li> <li><a href="namespaces.html">Namespaces</a></li> <li><a href="qtglobal.html">Global Declarations</a></li> <li><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </li> <li><a href="#">Qt Topics</a> <ul> <li><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li><a href="supported-platforms.html">Supported Platforms</a></li> <li><a href="technology-apis.html">Qt and Key Technologies</a></li> <li><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </li> <li><a href="#">Examples</a> <ul> <li><a href="all-examples.html">Examples</a></li> <li><a href="tutorials.html">Tutorials</a></li> <li><a href="demos.html">Demos</a></li> <li><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </li> </ul> </div> </div> <div class="wrapper"> <div class="hd"> <span></span> </div> <div class="bd group"> <div class="sidebar"> <div class="searchlabel"> Search index:</div> <div class="search" id="sidebarsearch"> <form id="qtdocsearch" action="" onsubmit="return false;"> <fieldset> <input type="text" name="searchstring" id="pageType" value="" /> <div id="resultdialog"> <a href="#" id="resultclose">Close</a> <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span> results:</p> <ul id="resultlist" class="all"> </ul> </div> </fieldset> </form> </div> <div class="box first bottombar" id="lookup"> <h2 title="API Lookup"><span></span> API Lookup</h2> <div id="list001" class="list"> <ul id="ul001" > <li class="defaultLink"><a href="classes.html">Class index</a></li> <li class="defaultLink"><a href="functions.html">Function index</a></li> <li class="defaultLink"><a href="modules.html">Modules</a></li> <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li> <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li> <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </div> </div> <div class="box bottombar" id="topics"> <h2 title="Qt Topics"><span></span> Qt Topics</h2> <div id="list002" class="list"> <ul id="ul002" > <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li class="defaultLink"><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li> <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </div> </div> <div class="box" id="examples"> <h2 title="Examples"><span></span> Examples</h2> <div id="list003" class="list"> <ul id="ul003"> <li class="defaultLink"><a href="all-examples.html">Examples</a></li> <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li> <li class="defaultLink"><a href="demos.html">Demos</a></li> <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </div> </div> </div> <div class="wrap"> <div class="toolbar"> <div class="breadcrumb toolblock"> <ul> <li class="first"><a href="index.html">Home</a></li> <!-- Breadcrumbs go here --> <li><a href="all-examples.html">Examples</a></li> <li>Default Prototypes Example</li> </ul> </div> <div class="toolbuttons toolblock"> <ul> <li id="smallA" class="t_button">A</li> <li id="medA" class="t_button active">A</li> <li id="bigA" class="t_button">A</li> <li id="print" class="t_button"><a href="javascript:this.print();"> <span>Print</span></a></li> </ul> </div> </div> <div class="content mainContent"> <h1 class="title">Default Prototypes Example</h1> <span class="subtitle"></span> <!-- $$$script/defaultprototypes-description --> <div class="descr"> <a name="details"></a> <p>Files:</p> <ul> <li><a href="script-defaultprototypes-code-js.html">script/defaultprototypes/code.js</a></li> <li><a href="script-defaultprototypes-prototypes-cpp.html">script/defaultprototypes/prototypes.cpp</a></li> <li><a href="script-defaultprototypes-prototypes-h.html">script/defaultprototypes/prototypes.h</a></li> <li><a href="script-defaultprototypes-main-cpp.html">script/defaultprototypes/main.cpp</a></li> <li><a href="script-defaultprototypes-defaultprototypes-pro.html">script/defaultprototypes/defaultprototypes.pro</a></li> <li><a href="script-defaultprototypes-defaultprototypes-qrc.html">script/defaultprototypes/defaultprototypes.qrc</a></li> </ul> <p>The Default Prototypes <a href="qtscript.html">QtScript</a> example shows how to use default prototypes to make a non-<a href="qobject.html">QObject</a>-based type scriptable.<p class="centerAlign"><img src="images/defaultprototypes-example.png" alt="" /></p><p>With <a href="qscriptengine.html#setDefaultPrototype">QScriptEngine::setDefaultPrototype</a>() you can specify a <a href="qtscript.html">QtScript</a> object that defines a scripting interface for a C++ type; Qt Script operations on values of such types will then be delegated to your prototype object. In this example, a simple scripting interface for <a href="qlistwidgetitem.html">QListWidgetItem</a> is defined, so that the text of items can easily be accessed from script code.</p> <p>To define a scripting API for <a href="qlistwidgetitem.html">QListWidgetItem</a> in terms of Qt properties and slots, we subclass <a href="qobject.html">QObject</a> and <a href="qscriptable.html">QScriptable</a>.</p> <pre class="cpp"> <span class="keyword">class</span> ListWidgetItemPrototype : <span class="keyword">public</span> <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">,</span> <span class="keyword">public</span> <span class="type"><a href="qscriptable.html">QScriptable</a></span> { Q_OBJECT Q_PROPERTY(<span class="type"><a href="qstring.html">QString</a></span> text READ text WRITE setText) <span class="keyword">public</span>: ListWidgetItemPrototype(<span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>); <span class="type"><a href="qstring.html">QString</a></span> text() <span class="keyword">const</span>; <span class="type">void</span> setText(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>text); <span class="keyword">public</span> <span class="keyword">slots</span>: <span class="type"><a href="qstring.html">QString</a></span> toString() <span class="keyword">const</span>; };</pre> <p>A single property, <tt>text</tt>, is defined, along with a slot, <tt>toString</tt>.</p> <pre class="cpp"> ListWidgetItemPrototype<span class="operator">::</span>ListWidgetItemPrototype(<span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>parent) : <span class="type"><a href="qobject.html">QObject</a></span>(parent) { } <span class="type"><a href="qstring.html">QString</a></span> ListWidgetItemPrototype<span class="operator">::</span>text() <span class="keyword">const</span> { <span class="type"><a href="qlistwidgetitem.html">QListWidgetItem</a></span> <span class="operator">*</span>item <span class="operator">=</span> qscriptvalue_cast<span class="operator"><</span><span class="type"><a href="qlistwidgetitem.html">QListWidgetItem</a></span><span class="operator">*</span><span class="operator">></span>(thisObject()); <span class="keyword">if</span> (item) <span class="keyword">return</span> item<span class="operator">-</span><span class="operator">></span>text(); <span class="keyword">return</span> <span class="type"><a href="qstring.html">QString</a></span>(); } <span class="type">void</span> ListWidgetItemPrototype<span class="operator">::</span>setText(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>text) { <span class="type"><a href="qlistwidgetitem.html">QListWidgetItem</a></span> <span class="operator">*</span>item <span class="operator">=</span> qscriptvalue_cast<span class="operator"><</span><span class="type"><a href="qlistwidgetitem.html">QListWidgetItem</a></span><span class="operator">*</span><span class="operator">></span>(thisObject()); <span class="keyword">if</span> (item) item<span class="operator">-</span><span class="operator">></span>setText(text); } <span class="type"><a href="qstring.html">QString</a></span> ListWidgetItemPrototype<span class="operator">::</span>toString() <span class="keyword">const</span> { <span class="keyword">return</span> <span class="type"><a href="qstring.html">QString</a></span>(<span class="string">"ListWidgetItem(text = %0)"</span>)<span class="operator">.</span>arg(text()); }</pre> <p>The implementation of the property accessors use the <a href="qscriptvalue.html#qscriptvalue_cast">qscriptvalue_cast</a>() function to cast the script object to a <a href="qlistwidgetitem.html">QListWidgetItem</a> pointer. The normal C++ <a href="qlistwidgetitem.html">QListWidgetItem</a> API is then used to implement the desired functionality.</p> <p>Although not shown here, it is possible to throw a script exception from a prototype function; for example, you could throw a TypeError exception if the <a href="qscriptvalue.html#qscriptvalue_cast">qscriptvalue_cast</a>() fails.</p> <p>QListWidgetItems are usually added to a <a href="qlistwidget.html">QListWidget</a>. While <a href="qlistwidget.html">QListWidget</a> is a <a href="qobject.html">QObject</a>-based class, not all the functionality needed for this example are present. We can solve this by creating a default prototype for the <a href="qlistwidget.html">QListWidget</a> class as well. The prototype will augment the functionality already provided by the Qt Script <a href="qobject.html">QObject</a> integration; i.e. if a property or slot is not found in the <a href="qlistwidget.html">QListWidget</a> object itself, the prototype will be used as a fallback.</p> <pre class="cpp"> <span class="keyword">class</span> ListWidgetPrototype : <span class="keyword">public</span> <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">,</span> <span class="keyword">public</span> <span class="type"><a href="qscriptable.html">QScriptable</a></span> { Q_OBJECT <span class="keyword">public</span>: ListWidgetPrototype(<span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>); <span class="keyword">public</span> <span class="keyword">slots</span>: <span class="type">void</span> addItem(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>text); <span class="type">void</span> addItems(<span class="keyword">const</span> <span class="type"><a href="qstringlist.html">QStringList</a></span> <span class="operator">&</span>texts); <span class="type">void</span> setBackgroundColor(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>colorName); };</pre> <p>The additional slots will make it possible to add items to a <a href="qlistwidget.html">QListWidget</a> from script code, and to set the background color of the widget from a string.</p> <pre class="cpp"> ListWidgetPrototype<span class="operator">::</span>ListWidgetPrototype(<span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>parent) : <span class="type"><a href="qobject.html">QObject</a></span>(parent) { } <span class="type">void</span> ListWidgetPrototype<span class="operator">::</span>addItem(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>text) { <span class="type"><a href="qlistwidget.html">QListWidget</a></span> <span class="operator">*</span>widget <span class="operator">=</span> qscriptvalue_cast<span class="operator"><</span><span class="type"><a href="qlistwidget.html">QListWidget</a></span><span class="operator">*</span><span class="operator">></span>(thisObject()); <span class="keyword">if</span> (widget) widget<span class="operator">-</span><span class="operator">></span>addItem(text); } <span class="type">void</span> ListWidgetPrototype<span class="operator">::</span>addItems(<span class="keyword">const</span> <span class="type"><a href="qstringlist.html">QStringList</a></span> <span class="operator">&</span>texts) { <span class="type"><a href="qlistwidget.html">QListWidget</a></span> <span class="operator">*</span>widget <span class="operator">=</span> qscriptvalue_cast<span class="operator"><</span><span class="type"><a href="qlistwidget.html">QListWidget</a></span><span class="operator">*</span><span class="operator">></span>(thisObject()); <span class="keyword">if</span> (widget) widget<span class="operator">-</span><span class="operator">></span>addItems(texts); } <span class="type">void</span> ListWidgetPrototype<span class="operator">::</span>setBackgroundColor(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>colorName) { <span class="type"><a href="qlistwidget.html">QListWidget</a></span> <span class="operator">*</span>widget <span class="operator">=</span> qscriptvalue_cast<span class="operator"><</span><span class="type"><a href="qlistwidget.html">QListWidget</a></span><span class="operator">*</span><span class="operator">></span>(thisObject()); <span class="keyword">if</span> (widget) { <span class="preprocessor">#ifdef Q_WS_MAEMO_5</span> <span class="type"><a href="qstring.html">QString</a></span> style <span class="operator">=</span> <span class="type"><a href="qstring.html">QString</a></span>(<span class="string">"QListWidget::item {background-color: %1;}"</span>)<span class="operator">.</span>arg(colorName); style <span class="operator">+</span><span class="operator">=</span> <span class="string">"QListWidget::item {selection-color: black;}"</span>; widget<span class="operator">-</span><span class="operator">></span>setStyleSheet(style); <span class="preprocessor">#else</span> <span class="type"><a href="qpalette.html">QPalette</a></span> palette <span class="operator">=</span> widget<span class="operator">-</span><span class="operator">></span>palette(); <span class="type"><a href="qcolor.html">QColor</a></span> color(colorName); palette<span class="operator">.</span>setBrush(<span class="type"><a href="qpalette.html">QPalette</a></span><span class="operator">::</span>Base<span class="operator">,</span> color); widget<span class="operator">-</span><span class="operator">></span>setPalette(palette); <span class="preprocessor">#endif</span> } }</pre> <p>Again, we use <a href="qscriptvalue.html#qscriptvalue_cast">qscriptvalue_cast</a>() to cast the script object to the relevant C++ type, in this case a <a href="qlistwidget.html">QListWidget</a> pointer. The addItem() and addItems() functions simply forward their arguments to the corresponding functions in the <a href="qlistwidget.html">QListWidget</a> class. setBackgroundColor() gets the widget's palette, creates a <a href="qcolor.html">QColor</a> from the given string argument and changes the palette accordingly.</p> <pre class="cpp"> <a href="qmetatype.html#Q_DECLARE_METATYPE">Q_DECLARE_METATYPE</a>(<span class="type"><a href="qlistwidgetitem.html">QListWidgetItem</a></span><span class="operator">*</span>) <a href="qmetatype.html#Q_DECLARE_METATYPE">Q_DECLARE_METATYPE</a>(<span class="type"><a href="qlistwidget.html">QListWidget</a></span><span class="operator">*</span>)</pre> <p>The relevant C++ types must be made known to Qt's meta type system.</p> <pre class="cpp"> <span class="type"><a href="qscriptengine.html">QScriptEngine</a></span> engine; ListWidgetItemPrototype lwiProto; engine<span class="operator">.</span>setDefaultPrototype(<a href="qmetatype.html#qMetaTypeId">qMetaTypeId</a><span class="operator"><</span><span class="type"><a href="qlistwidgetitem.html">QListWidgetItem</a></span><span class="operator">*</span><span class="operator">></span>()<span class="operator">,</span> engine<span class="operator">.</span>newQObject(<span class="operator">&</span>lwiProto)); ListWidgetPrototype lwProto; engine<span class="operator">.</span>setDefaultPrototype(<a href="qmetatype.html#qMetaTypeId">qMetaTypeId</a><span class="operator"><</span><span class="type"><a href="qlistwidget.html">QListWidget</a></span><span class="operator">*</span><span class="operator">></span>()<span class="operator">,</span> engine<span class="operator">.</span>newQObject(<span class="operator">&</span>lwProto));</pre> <p>For each type that we want to associate a prototype object with, we create an instance of the prototype class, pass it to <a href="qscriptengine.html#newQObject">QScriptEngine::newQObject</a>(), and then create the link between the C++ type and the resulting script object by calling <a href="qscriptengine.html#setDefaultPrototype">QScriptEngine::setDefaultPrototype</a>().</p> <pre class="cpp"> <span class="type"><a href="qlistwidget.html">QListWidget</a></span> listWidget; engine<span class="operator">.</span>globalObject()<span class="operator">.</span>setProperty(<span class="string">"listWidget"</span><span class="operator">,</span> engine<span class="operator">.</span>newQObject(<span class="operator">&</span>listWidget));</pre> <p>In this example, a single <a href="qlistwidget.html">QListWidget</a> object is added as a global script variable, called <tt>listWidget</tt>. Script code can add items to this widget by calling addItem() or addItems().</p> <pre class="js"> <span class="name">listWidget</span>.<span class="name">addItem</span>(<span class="string">"Red"</span>); <span class="name">listWidget</span>.<span class="name">addItem</span>(<span class="string">"Blue"</span>); <span class="name">listWidget</span>.<span class="name">addItem</span>(<span class="string">"Green"</span>); <span class="name">listWidget</span>.<span class="name">addItem</span>(<span class="string">"Cyan"</span>); <span class="name">listWidget</span>.<span class="name">addItem</span>(<span class="string">"Yellow"</span>); <span class="name">listWidget</span>.<span class="name">addItem</span>(<span class="string">"Purple"</span>); <span class="name">listWidget</span>.<span class="name">addItems</span>([<span class="string">"Orange"</span>, <span class="string">"Gray"</span>]);</pre> <p>Script code can connect to signals of the <a href="qlistwidget.html">QListWidget</a> object; signal handlers can use the interface defined in the <a href="qlistwidgetitem.html">QListWidgetItem</a> prototype to manipulate item arguments.</p> <pre class="js"> <span class="name">listWidget</span>.<span class="name">currentItemChanged</span>.<span class="name">connect</span>( <span class="keyword">function</span>(<span class="name">item</span>) { <span class="name">listWidget</span>.<span class="name">setBackgroundColor</span>(<span class="name">item</span>.<span class="name">text</span>); } );</pre> <p>Not shown in this example is how to make <a href="qlistwidgetitem.html">QListWidgetItem</a> constructible from Qt Script code, i.e. to be able to write "new QListWidgetItem()" in a script. In order to do this, you have to define your own script constructor for the type. The constructor would just be a factory function that constructs a new C++ <a href="qlistwidgetitem.html">QListWidgetItem</a> and returns it back to the script. See <a href="qscriptengine.html#newFunction">QScriptEngine::newFunction</a>() for more information.</p> </div> <!-- @@@script/defaultprototypes --> </div> </div> </div> <div class="ft"> <span></span> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2015 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.</p> <br /> <p> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.</p> <p> Documentation sources may be obtained from <a href="http://www.qt-project.org"> www.qt-project.org</a>.</p> <br /> <p> Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. <a title="Privacy Policy" href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p> </div> <script src="scripts/functions.js" type="text/javascript"></script> </body> </html>