File: threads-queuedcustomtype.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 (249 lines) | stat: -rw-r--r-- 18,935 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?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/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>Queued Custom Type 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-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">&amp;</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">&amp;</span>rect<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qcolor.html">QColor</a></span> <span class="operator">&amp;</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">&amp;</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">&amp;</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">&gt;</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">&quot;&amp;Load image...&quot;</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">&quot;&amp;Stop&quot;</span>));
     resetButton<span class="operator">-</span><span class="operator">&gt;</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">&quot;Queued Custom Type&quot;</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">&amp;</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">&amp;</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">&gt;</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">&amp;</span>image);

 <span class="keyword">signals</span>:
     <span class="type">void</span> sendBlock(<span class="keyword">const</span> Block <span class="operator">&amp;</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">&amp;</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">&gt;</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">&lt;</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">&lt;</span>Block<span class="operator">&gt;</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 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>