<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- defaultprototypes.qdoc --> <title>Default Prototypes Example | Qt Script</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qtscript-index.html">Qt Script</a></td><td >Default Prototypes Example</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtscript-index.html">Qt 5.12.6 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Default Prototypes Example</h1> <span class="subtitle"></span> <!-- $$$script/defaultprototypes-description --> <div class="descr"> <a name="details"></a> <p>This Qt Script example shows how to use default prototypes to make a non-QObject-based type scriptable.</p> <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 Qt Script 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 QListWidgetItem is defined, so that the text of items can easily be accessed from script code.</p> <p>To define a scripting API for QListWidgetItem in terms of Qt properties and slots, we subclass QObject 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">QObject</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">QString</span> text READ text WRITE setText) <span class="keyword">public</span>: ListWidgetItemPrototype(<span class="type">QObject</span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>); <span class="type">QString</span> text() <span class="keyword">const</span>; <span class="type">void</span> setText(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>text); <span class="keyword">public</span> <span class="keyword">slots</span>: <span class="type">QString</span> toString() <span class="keyword">const</span>; }; </pre> <p>A single property, <code>text</code>, is defined, along with a slot, <code>toString</code>.</p> <pre class="cpp"> ListWidgetItemPrototype<span class="operator">::</span>ListWidgetItemPrototype(<span class="type">QObject</span> <span class="operator">*</span>parent) : <span class="type">QObject</span>(parent) { } <span class="type">QString</span> ListWidgetItemPrototype<span class="operator">::</span>text() <span class="keyword">const</span> { <span class="type">QListWidgetItem</span> <span class="operator">*</span>item <span class="operator">=</span> qscriptvalue_cast<span class="operator"><</span><span class="type">QListWidgetItem</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">QString</span>(); } <span class="type">void</span> ListWidgetItemPrototype<span class="operator">::</span>setText(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>text) { <span class="type">QListWidgetItem</span> <span class="operator">*</span>item <span class="operator">=</span> qscriptvalue_cast<span class="operator"><</span><span class="type">QListWidgetItem</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">QString</span> ListWidgetItemPrototype<span class="operator">::</span>toString() <span class="keyword">const</span> { <span class="keyword">return</span> <span class="type">QString</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 QListWidgetItem pointer. The normal C++ QListWidgetItem 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 QListWidget. While QListWidget is a QObject-based class, not all the functionality needed for this example are present. We can solve this by creating a default prototype for the QListWidget class as well. The prototype will augment the functionality already provided by the Qt Script QObject integration; i.e. if a property or slot is not found in the QListWidget 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">QObject</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">QObject</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">QString</span> <span class="operator">&</span>text); <span class="type">void</span> addItems(<span class="keyword">const</span> <span class="type">QStringList</span> <span class="operator">&</span>texts); <span class="type">void</span> setBackgroundColor(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>colorName); }; </pre> <p>The additional slots will make it possible to add items to a QListWidget 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">QObject</span> <span class="operator">*</span>parent) : <span class="type">QObject</span>(parent) { } <span class="type">void</span> ListWidgetPrototype<span class="operator">::</span>addItem(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>text) { <span class="type">QListWidget</span> <span class="operator">*</span>widget <span class="operator">=</span> qscriptvalue_cast<span class="operator"><</span><span class="type">QListWidget</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">QStringList</span> <span class="operator">&</span>texts) { <span class="type">QListWidget</span> <span class="operator">*</span>widget <span class="operator">=</span> qscriptvalue_cast<span class="operator"><</span><span class="type">QListWidget</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">QString</span> <span class="operator">&</span>colorName) { <span class="type">QListWidget</span> <span class="operator">*</span>widget <span class="operator">=</span> qscriptvalue_cast<span class="operator"><</span><span class="type">QListWidget</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">QString</span> style <span class="operator">=</span> <span class="type">QString</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">QPalette</span> palette <span class="operator">=</span> widget<span class="operator">-</span><span class="operator">></span>palette(); <span class="type">QColor</span> color(colorName); palette<span class="operator">.</span>setBrush(<span class="type">QPalette</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 QListWidget pointer. The addItem() and addItems() functions simply forward their arguments to the corresponding functions in the QListWidget class. setBackgroundColor() gets the widget's palette, creates a QColor from the given string argument and changes the palette accordingly.</p> <pre class="cpp"> Q_DECLARE_METATYPE(<span class="type">QListWidgetItem</span><span class="operator">*</span>) Q_DECLARE_METATYPE(<span class="type">QListWidget</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(qMetaTypeId<span class="operator"><</span><span class="type">QListWidgetItem</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(qMetaTypeId<span class="operator"><</span><span class="type">QListWidget</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">QListWidget</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 QListWidget object is added as a global script variable, called <code>listWidget</code>. Script code can add items to this widget by calling addItem() or addItems().</p> <pre class="js"> listWidget.addItem("Red"); listWidget.addItem("Blue"); listWidget.addItem("Green"); listWidget.addItem("Cyan"); listWidget.addItem("Yellow"); listWidget.addItem("Purple"); listWidget.addItems(["Orange", "Gray"]); </pre> <p>Script code can connect to signals of the QListWidget object; signal handlers can use the interface defined in the QListWidgetItem prototype to manipulate item arguments.</p> <pre class="js"> listWidget.currentItemChanged.connect( function(item) { listWidget.setBackgroundColor(item.text); } ); </pre> <p>Not shown in this example is how to make QListWidgetItem 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++ QListWidgetItem and returns it back to the script. See <a href="qscriptengine.html#newFunction">QScriptEngine::newFunction</a>() for more information.</p> <p>Files:</p> <ul> <li><a href="qtscript-script-defaultprototypes-code-js.html">script/defaultprototypes/code.js</a></li> <li><a href="qtscript-script-defaultprototypes-defaultprototypes-pro.html">script/defaultprototypes/defaultprototypes.pro</a></li> <li><a href="qtscript-script-defaultprototypes-defaultprototypes-qrc.html">script/defaultprototypes/defaultprototypes.qrc</a></li> <li><a href="qtscript-script-defaultprototypes-main-cpp.html">script/defaultprototypes/main.cpp</a></li> <li><a href="qtscript-script-defaultprototypes-prototypes-cpp.html">script/defaultprototypes/prototypes.cpp</a></li> <li><a href="qtscript-script-defaultprototypes-prototypes-h.html">script/defaultprototypes/prototypes.h</a></li> </ul> </div> <!-- @@@script/defaultprototypes --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> 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.<br/> 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. </p> </div> </body> </html>