File: tools-customtypesending.html

package info (click to toggle)
qt4-x11 4%3A4.8.2%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 701,696 kB
  • sloc: cpp: 2,686,179; ansic: 375,485; python: 25,859; sh: 19,349; xml: 17,091; perl: 14,765; yacc: 5,383; asm: 5,038; makefile: 1,259; lex: 555; ruby: 526; objc: 347; cs: 112; pascal: 112; php: 54; sed: 34
file content (187 lines) | stat: -rw-r--r-- 13,654 bytes parent folder | download
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?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/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="content"> 
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
  </div>
  <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>
<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">&amp;</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">&amp;</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">&amp;</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">&amp;</span>body<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qstringlist.html">QStringList</a></span> <span class="operator">&amp;</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">&quot;&amp;Send message&quot;</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">&gt;</span>addStretch();
     buttonLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(sendButton);
     buttonLayout<span class="operator">-</span><span class="operator">&gt;</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">&gt;</span>addWidget(editor);
     layout<span class="operator">-</span><span class="operator">&gt;</span>addLayout(buttonLayout);

     setWindowTitle(tr(<span class="string">&quot;Custom Type Sending&quot;</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">&gt;</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">&amp;</span>message)
 {
     thisMessage <span class="operator">=</span> message;
     editor<span class="operator">-</span><span class="operator">&gt;</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">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Subject: Hello World&quot;</span>
             <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;From: qt-info@nokia.com&quot;</span>;
     <span class="type"><a href="qstring.html">QString</a></span> body <span class="operator">=</span> <span class="string">&quot;This is a test.\r\n&quot;</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">&amp;</span>window1<span class="operator">,</span> SIGNAL(messageSent(Message))<span class="operator">,</span>
                      <span class="operator">&amp;</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">&amp;</span>window2<span class="operator">,</span> SIGNAL(messageSent(Message))<span class="operator">,</span>
                      <span class="operator">&amp;</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 class="ft">
    <span></span>
  </div>
</div> 
<div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2012 Nokia Corporation 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>
      Nokia, Qt and their respective logos are trademarks of Nokia Corporation 
      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>
</body>
</html>