<?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" /> <!-- customtypesending.qdoc --> <title>Qt 4.8: Custom Type Sending 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>Custom Type Sending 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-window-and-message-class-definitions">The Window and Message Class Definitions</a></li> <li class="level1"><a href="#the-window-class-implementation">The Window Class Implementation</a></li> <li class="level1"><a href="#making-the-connection">Making the Connection</a></li> <li class="level1"><a href="#further-reading">Further Reading</a></li> </ul> </div> <h1 class="title">Custom Type Sending Example</h1> <span class="subtitle"></span> <!-- $$$tools/customtypesending-description --> <div class="descr"> <a name="details"></a> <p>Files:</p> <ul> <li><a href="tools-customtypesending-message-cpp.html">tools/customtypesending/message.cpp</a></li> <li><a href="tools-customtypesending-message-h.html">tools/customtypesending/message.h</a></li> <li><a href="tools-customtypesending-window-cpp.html">tools/customtypesending/window.cpp</a></li> <li><a href="tools-customtypesending-window-h.html">tools/customtypesending/window.h</a></li> <li><a href="tools-customtypesending-main-cpp.html">tools/customtypesending/main.cpp</a></li> <li><a href="tools-customtypesending-customtypesending-pro.html">tools/customtypesending/customtypesending.pro</a></li> </ul> <p>The Custom Type Sending example shows how to use a custom type with signals and slots.<p class="centerAlign"><img src="images/customtypesending-example.png" alt="" /></p><a name="overview"></a> <h2>Overview</h2> <p>In the <a href="tools-customtype.html">Custom Type Example</a>, we showed how to integrate custom types with the meta-object system, enabling them to be stored in <a href="qvariant.html">QVariant</a> objects, written out in debugging information and used in signal-slot communication.</p> <p>In this example, we demonstrate that the preparations made to the <tt>Message</tt> class and its declaration with <a href="qmetatype.html#Q_DECLARE_METATYPE">Q_DECLARE_METATYPE</a>() enable it to be used with direct signal-slot connections. We do this by creating a <tt>Window</tt> class containing signals and slots whose signatures include <tt>Message</tt> arguments.</p> <a name="the-window-and-message-class-definitions"></a> <h2>The Window and Message Class Definitions</h2> <p>We define a simple <tt>Window</tt> class with a signal and public slot that allow a <tt>Message</tt> object to be sent via a signal-slot connection:</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="keyword">signals</span>: <span class="type">void</span> messageSent(<span class="keyword">const</span> Message <span class="operator">&</span>message); <span class="keyword">public</span> <span class="keyword">slots</span>: <span class="type">void</span> setMessage(<span class="keyword">const</span> Message <span class="operator">&</span>message); <span class="keyword">private</span> <span class="keyword">slots</span>: <span class="type">void</span> sendMessage(); <span class="keyword">private</span>: Message thisMessage; <span class="type"><a href="qtextedit.html">QTextEdit</a></span> <span class="operator">*</span>editor; };</pre> <p>The window will contain a text editor to show the contents of a message and a push button that the user can click to send a message. To facilitate this, we also define the <tt>sendMessage()</tt> slot. We also keep a <tt>Message</tt> instance in the <tt>thisMessage</tt> private variable which holds the actual message to be sent.</p> <p>The <tt>Message</tt> class is defined in the following way:</p> <pre class="cpp"> <span class="keyword">class</span> Message { <span class="keyword">public</span>: Message(); Message(<span class="keyword">const</span> Message <span class="operator">&</span>other); <span class="operator">~</span>Message(); Message(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>body<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qstringlist.html">QStringList</a></span> <span class="operator">&</span>headers); <span class="type"><a href="qstring.html">QString</a></span> body() <span class="keyword">const</span>; <span class="type"><a href="qstringlist.html">QStringList</a></span> headers() <span class="keyword">const</span>; <span class="keyword">private</span>: <span class="type"><a href="qstring.html">QString</a></span> m_body; <span class="type"><a href="qstringlist.html">QStringList</a></span> m_headers; };</pre> <p>The type is declared to the meta-type system with the <a href="qmetatype.html#Q_DECLARE_METATYPE">Q_DECLARE_METATYPE</a>() macro:</p> <pre class="cpp"> <a href="qmetatype.html#Q_DECLARE_METATYPE">Q_DECLARE_METATYPE</a>(Message);</pre> <p>This will make the type available for use in direct signal-slot connections.</p> <a name="the-window-class-implementation"></a> <h2>The Window Class Implementation</h2> <p>The <tt>Window</tt> constructor sets up a user interface containing a text editor and a push button.</p> <pre class="cpp"> Window<span class="operator">::</span>Window() { editor <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtextedit.html">QTextEdit</a></span>(); <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>sendButton <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(tr(<span class="string">"&Send message"</span>)); connect(sendButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(sendMessage())); <span class="type"><a href="qhboxlayout.html">QHBoxLayout</a></span> <span class="operator">*</span>buttonLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qhboxlayout.html">QHBoxLayout</a></span>(); buttonLayout<span class="operator">-</span><span class="operator">></span>addStretch(); buttonLayout<span class="operator">-</span><span class="operator">></span>addWidget(sendButton); buttonLayout<span class="operator">-</span><span class="operator">></span>addStretch(); <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>layout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span>(<span class="keyword">this</span>); layout<span class="operator">-</span><span class="operator">></span>addWidget(editor); layout<span class="operator">-</span><span class="operator">></span>addLayout(buttonLayout); setWindowTitle(tr(<span class="string">"Custom Type Sending"</span>)); }</pre> <p>The button's <a href="qabstractbutton.html#clicked">clicked()</a> signal is connected to the window's <tt>sendMessage()</tt> slot, which emits the <tt>messageSent(Message)</tt> signal with the <tt>Message</tt> held by the <tt>thisMessage</tt> variable:</p> <pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>sendMessage() { thisMessage <span class="operator">=</span> Message(editor<span class="operator">-</span><span class="operator">></span>toPlainText()<span class="operator">,</span> thisMessage<span class="operator">.</span>headers()); <span class="keyword">emit</span> messageSent(thisMessage); }</pre> <p>We implement a slot to allow the message to be received, and this also lets us set the message in the window programatically:</p> <pre class="cpp"> <span class="type">void</span> Window<span class="operator">::</span>setMessage(<span class="keyword">const</span> Message <span class="operator">&</span>message) { thisMessage <span class="operator">=</span> message; editor<span class="operator">-</span><span class="operator">></span>setPlainText(thisMessage<span class="operator">.</span>body()); }</pre> <p>In this function, we simply assign the new message to <tt>thisMessage</tt> and update the text in the editor.</p> <a name="making-the-connection"></a> <h2>Making the Connection</h2> <p>In the example's <tt>main()</tt> function, we perform the connection between two instances of the <tt>Window</tt> class:</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); Window window1; <span class="type"><a href="qstringlist.html">QStringList</a></span> headers; headers <span class="operator"><</span><span class="operator"><</span> <span class="string">"Subject: Hello World"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"From: qt-info@nokia.com"</span>; <span class="type"><a href="qstring.html">QString</a></span> body <span class="operator">=</span> <span class="string">"This is a test.\r\n"</span>; Message message(body<span class="operator">,</span> headers); window1<span class="operator">.</span>setMessage(message); Window window2; <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect(<span class="operator">&</span>window1<span class="operator">,</span> SIGNAL(messageSent(Message))<span class="operator">,</span> <span class="operator">&</span>window2<span class="operator">,</span> SLOT(setMessage(Message))); <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect(<span class="operator">&</span>window2<span class="operator">,</span> SIGNAL(messageSent(Message))<span class="operator">,</span> <span class="operator">&</span>window1<span class="operator">,</span> SLOT(setMessage(Message))); window1<span class="operator">.</span>show(); window2<span class="operator">.</span>show(); <span class="keyword">return</span> app<span class="operator">.</span>exec(); }</pre> <p>We set the message for the first window and connect the <tt>messageSent(Message)</tt> signal from each window to the other's <tt>setMessage(Message)</tt> slot. Since the signals and slots mechanism is only concerned with the type, we can simplify the signatures of both the signal and slot when we make the connection.</p> <p>When the user clicks on the <b>Send message</b> button in either window, the message shown will be emitted in a signal that the other window will receive and display.</p> <a name="further-reading"></a> <h2>Further Reading</h2> <p>Although the custom <tt>Message</tt> type can be used with direct signals and slots, an additional registration step needs to be performed if you want to use it with queued signal-slot connections. See the <a href="threads-queuedcustomtype.html">Queued Custom Type Example</a> for details.</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> <!-- @@@tools/customtypesending --> </div> </div> </div> <div class="ft"> <span></span> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2013 Digia Plc and/or its subsidiaries. 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> Digia, Qt and their respective logos are trademarks of Digia Plc 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>