<?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" /> <!-- queuedcustomtype.qdoc --> <title>Qt 4.8: Queued Custom Type 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>Queued Custom Type 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"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#overview">Overview</a></li> <li class="level1"><a href="#the-block-class">The Block Class</a></li> <li class="level1"><a href="#the-window-class">The Window Class</a></li> <li class="level1"><a href="#the-renderthread-class">The RenderThread Class</a></li> <li class="level1"><a href="#registering-the-type">Registering the Type</a></li> <li class="level1"><a href="#further-reading">Further Reading</a></li> </ul> </div> <h1 class="title">Queued Custom Type Example</h1> <span class="subtitle"></span> <!-- $$$threads/queuedcustomtype-description --> <div class="descr"> <a name="details"></a> <p>Files:</p> <ul> <li><a href="threads-queuedcustomtype-block-cpp.html">threads/queuedcustomtype/block.cpp</a></li> <li><a href="threads-queuedcustomtype-block-h.html">threads/queuedcustomtype/block.h</a></li> <li><a href="threads-queuedcustomtype-renderthread-cpp.html">threads/queuedcustomtype/renderthread.cpp</a></li> <li><a href="threads-queuedcustomtype-renderthread-h.html">threads/queuedcustomtype/renderthread.h</a></li> <li><a href="threads-queuedcustomtype-window-cpp.html">threads/queuedcustomtype/window.cpp</a></li> <li><a href="threads-queuedcustomtype-window-h.html">threads/queuedcustomtype/window.h</a></li> <li><a href="threads-queuedcustomtype-main-cpp.html">threads/queuedcustomtype/main.cpp</a></li> <li><a href="threads-queuedcustomtype-queuedcustomtype-pro.html">threads/queuedcustomtype/queuedcustomtype.pro</a></li> </ul> <p>The Queued Custom Type example shows how to send custom types between threads with queued signals and slots.<p class="centerAlign"><img src="images/queuedcustomtype-example.png" alt="" /></p><p>Contents:</p> <a name="overview"></a> <h2>Overview</h2> <p>In the <a href="tools-customtypesending.html">Custom Type Sending Example</a>, we showed how to use a custom type with signal-slot communication within the same thread.</p> <p>In this example, we create a new value class, <tt>Block</tt>, and register it with the meta-object system to enable us to send instances of it between threads using queued signals and slots.</p> <a name="the-block-class"></a> <h2>The Block Class</h2> <p>The <tt>Block</tt> class is similar to the <tt>Message</tt> class described in the <a href="tools-customtype.html">Custom Type Example</a>. It provides the default constructor, copy constructor and destructor in the public section of the class that the meta-object system requires. It describes a colored rectangle.</p> <pre class="cpp"> <span class="keyword">class</span> Block { <span class="keyword">public</span>: Block(); Block(<span class="keyword">const</span> Block <span class="operator">&</span>other); <span class="operator">~</span>Block(); Block(<span class="keyword">const</span> <span class="type"><a href="qrect.html">QRect</a></span> <span class="operator">&</span>rect<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qcolor.html">QColor</a></span> <span class="operator">&</span>color); <span class="type"><a href="qcolor.html">QColor</a></span> color() <span class="keyword">const</span>; <span class="type"><a href="qrect.html">QRect</a></span> rect() <span class="keyword">const</span>; <span class="keyword">private</span>: <span class="type"><a href="qrect.html">QRect</a></span> m_rect; <span class="type"><a href="qcolor.html">QColor</a></span> m_color; }; <a href="qmetatype.html#Q_DECLARE_METATYPE">Q_DECLARE_METATYPE</a>(Block);</pre> <p>We will still need to register it with the meta-object system at run-time by calling the <a href="qmetatype.html#qRegisterMetaType">qRegisterMetaType</a>() template function before we make any signal-slot connections that use this type. Even though we do not intend to use the type with <a href="qvariant.html">QVariant</a> in this example, it is good practice to also declare the new type with <a href="qmetatype.html#Q_DECLARE_METATYPE">Q_DECLARE_METATYPE</a>().</p> <p>The implementation of the <tt>Block</tt> class is trivial, so we avoid quoting it here.</p> <a name="the-window-class"></a> <h2>The Window Class</h2> <p>We define a simple <tt>Window</tt> class with a public slot that accepts a <tt>Block</tt> object. The rest of the class is concerned with managing the user interface and handling images.</p> <pre class="cpp"> <span class="keyword">class</span> Window : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span> { Q_OBJECT <span class="keyword">public</span>: Window(); <span class="type">void</span> loadImage(<span class="keyword">const</span> <span class="type"><a href="qimage.html">QImage</a></span> <span class="operator">&</span>image); <span class="keyword">public</span> <span class="keyword">slots</span>: <span class="type">void</span> addBlock(<span class="keyword">const</span> Block <span class="operator">&</span>block); <span class="keyword">private</span> <span class="keyword">slots</span>: <span class="type">void</span> loadImage(); <span class="type">void</span> resetUi(); <span class="keyword">private</span>: <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>label; <span class="type"><a href="qpixmap.html">QPixmap</a></span> pixmap; <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>loadButton; <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>resetButton; <span class="type"><a href="qstring.html">QString</a></span> path; RenderThread <span class="operator">*</span>thread; };</pre> <p>The <tt>Window</tt> class also contains a worker thread, provided by a <tt>RenderThread</tt> object. This will emit signals to send <tt>Block</tt> objects to the window's <tt>addBlock(Block)</tt> slot.</p> <p>The parts of the <tt>Window</tt> class that are most relevant are the constructor and the <tt>addBlock(Block)</tt> slot.</p> <p>The constructor creates a thread for rendering images, sets up a user interface containing a label and two push buttons that are connected to slots in the same class.</p> <pre class="cpp"> Window<span class="operator">::</span>Window() { thread <span class="operator">=</span> <span class="keyword">new</span> RenderThread(); label <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(); label<span class="operator">-</span><span class="operator">></span>setAlignment(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>AlignCenter); loadButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">"&Load image..."</span>)); resetButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">"&Stop"</span>)); resetButton<span class="operator">-</span><span class="operator">></span>setEnabled(<span class="keyword">false</span>); connect(loadButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(loadImage())); connect(resetButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> thread<span class="operator">,</span> SLOT(stopProcess())); connect(thread<span class="operator">,</span> SIGNAL(finished())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(resetUi())); connect(thread<span class="operator">,</span> SIGNAL(sendBlock(Block))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(addBlock(Block)));</pre> <p>In the last of these connections, we connect a signal in the <tt>RenderThread</tt> object to the <tt>addBlock(Block)</tt> slot in the window.</p> <pre class="qml"> ... setWindowTitle(tr(<span class="string">"Queued Custom Type"</span>)); }</pre> <p>The rest of the constructor simply sets up the layout of the window.</p> <p>The <tt>addBlock(Block)</tt> slot receives blocks from the rendering thread via the signal-slot connection set up in the constructor:</p> <pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>addBlock(<span class="keyword">const</span> Block <span class="operator">&</span>block) { <span class="type"><a href="qcolor.html">QColor</a></span> color <span class="operator">=</span> block<span class="operator">.</span>color(); color<span class="operator">.</span>setAlpha(<span class="number">64</span>); <span class="type"><a href="qpainter.html">QPainter</a></span> painter; painter<span class="operator">.</span>begin(<span class="operator">&</span>pixmap); painter<span class="operator">.</span>fillRect(block<span class="operator">.</span>rect()<span class="operator">,</span> color); painter<span class="operator">.</span>end(); label<span class="operator">-</span><span class="operator">></span>setPixmap(pixmap); }</pre> <p>We simply paint these onto the label as they arrive.</p> <a name="the-renderthread-class"></a> <h2>The RenderThread Class</h2> <p>The <tt>RenderThread</tt> class processes an image, creating <tt>Block</tt> objects and using the <tt>sendBlock(Block)</tt> signal to send them to other components in the example.</p> <pre class="cpp"> <span class="keyword">class</span> RenderThread : <span class="keyword">public</span> <span class="type"><a href="qthread.html">QThread</a></span> { Q_OBJECT <span class="keyword">public</span>: RenderThread(<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="operator">~</span>RenderThread(); <span class="type">void</span> processImage(<span class="keyword">const</span> <span class="type"><a href="qimage.html">QImage</a></span> <span class="operator">&</span>image); <span class="keyword">signals</span>: <span class="type">void</span> sendBlock(<span class="keyword">const</span> Block <span class="operator">&</span>block); <span class="keyword">public</span> <span class="keyword">slots</span>: <span class="type">void</span> stopProcess(); <span class="keyword">protected</span>: <span class="type">void</span> run(); <span class="keyword">private</span>: <span class="type">bool</span> m_abort; <span class="type"><a href="qimage.html">QImage</a></span> m_image; <span class="type"><a href="qmutex.html">QMutex</a></span> mutex; };</pre> <p>The constructor and destructor are not quoted here. These take care of setting up the thread's internal state and cleaning up when it is destroyed.</p> <p>Processing is started with the <tt>processImage()</tt> function, which calls the <tt>RenderThread</tt> class's reimplementation of the <a href="qthread.html#run">QThread::run</a>() function:</p> <pre class="cpp"> <span class="type">void</span> RenderThread<span class="operator">::</span>processImage(<span class="keyword">const</span> <span class="type"><a href="qimage.html">QImage</a></span> <span class="operator">&</span>image) { <span class="keyword">if</span> (image<span class="operator">.</span>isNull()) <span class="keyword">return</span>; m_image <span class="operator">=</span> image; m_abort <span class="operator">=</span> <span class="keyword">false</span>; start(); } <span class="type">void</span> RenderThread<span class="operator">::</span>run() { <span class="type">int</span> size <span class="operator">=</span> <a href="qtglobal.html#qMax">qMax</a>(m_image<span class="operator">.</span>width()<span class="operator">/</span><span class="number">20</span><span class="operator">,</span> m_image<span class="operator">.</span>height()<span class="operator">/</span><span class="number">20</span>); <span class="keyword">for</span> (<span class="type">int</span> s <span class="operator">=</span> size; s <span class="operator">></span> <span class="number">0</span>; <span class="operator">-</span><span class="operator">-</span>s) { <span class="keyword">for</span> (<span class="type">int</span> c <span class="operator">=</span> <span class="number">0</span>; c <span class="operator"><</span> <span class="number">400</span>; <span class="operator">+</span><span class="operator">+</span>c) {</pre> <p>Ignoring the details of the way the image is processed, we see that the signal containing a block is emitted in the usual way:</p> <pre class="qml"> ... Block block(<span class="type"><a href="qrect.html">QRect</a></span>(x1<span class="operator">,</span> y1<span class="operator">,</span> x2 <span class="operator">-</span> x1 <span class="operator">+</span> <span class="number">1</span><span class="operator">,</span> y2 <span class="operator">-</span> y1 <span class="operator">+</span> <span class="number">1</span>)<span class="operator">,</span> <span class="type"><a href="qcolor.html">QColor</a></span>(red<span class="operator">/</span>n<span class="operator">,</span> green<span class="operator">/</span>n<span class="operator">,</span> blue<span class="operator">/</span>n)); <span class="keyword">emit</span> sendBlock(block); <span class="keyword">if</span> (m_abort) <span class="keyword">return</span>; msleep(<span class="number">10</span>); } } }</pre> <p>Each signal that is emitted will be queued and delivered later to the window's <tt>addBlock(Block)</tt> slot.</p> <a name="registering-the-type"></a> <h2>Registering the Type</h2> <p>In the example's <tt>main()</tt> function, we perform the registration of the <tt>Block</tt> class as a custom type with the meta-object system by calling the <a href="qmetatype.html#qRegisterMetaType">qRegisterMetaType</a>() template function:</p> <pre class="cpp"> <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>) { <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv); <a href="qmetatype.html#qRegisterMetaType">qRegisterMetaType</a><span class="operator"><</span>Block<span class="operator">></span>(); qsrand(<span class="type"><a href="qtime.html">QTime</a></span><span class="operator">::</span>currentTime()<span class="operator">.</span>elapsed()); Window window; <span class="preprocessor">#if defined(Q_WS_S60)</span> window<span class="operator">.</span>showMaximized(); <span class="preprocessor">#else</span> window<span class="operator">.</span>show(); <span class="preprocessor">#endif</span> window<span class="operator">.</span>loadImage(createImage(<span class="number">256</span><span class="operator">,</span> <span class="number">256</span>)); <span class="keyword">return</span> app<span class="operator">.</span>exec(); }</pre> <p>This call is placed here to ensure that the type is registered before any signal-slot connections are made that use it.</p> <p>The rest of the <tt>main()</tt> function is concerned with setting a seed for the pseudo-random number generator, creating and showing the window, and setting a default image. See the source code for the implementation of the <tt>createImage()</tt> function.</p> <a name="further-reading"></a> <h2>Further Reading</h2> <p>This example showed how a custom type can be registered with the meta-object system so that it can be used with signal-slot connections between threads. For ordinary communication involving direct signals and slots, it is enough to simply declare the type in the way described in the <a href="tools-customtypesending.html">Custom Type Sending Example</a>.</p> <p>In practice, both the <a href="qmetatype.html#Q_DECLARE_METATYPE">Q_DECLARE_METATYPE</a>() macro and the <a href="qmetatype.html#qRegisterMetaType">qRegisterMetaType</a>() template function can be used to register custom types, but <a href="qmetatype.html#qRegisterMetaType">qRegisterMetaType</a>() is only required if you need to perform signal-slot communication or need to create and destroy objects of the custom type at run-time.</p> <p>More information on using custom types with Qt can be found in the <a href="custom-types.html">Creating Custom Qt Types</a> document.</p> </div> <!-- @@@threads/queuedcustomtype --> </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>