3
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
<!-- /tmp/qt-4.1.4-harald-1150713676011/qt-x11-opensource-src-4.1.4/doc/src/qsignalspy.qdoc -->
6
<!-- /tmp/qt-4.1.4-harald-1150713676011/qt-x11-opensource-src-4.1.4/doc/src/qsignalspy.qdoc -->
5
<!-- /tmp/qt-4.2.0-qt-1159540607313/qt-x11-opensource-src-4.2.0/doc/src/qsignalspy.qdoc -->
8
<title>Qt 4.1: QSignalSpy Class Reference</title>
9
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
10
a:link { color: #004faf; text-decoration: none }
11
a:visited { color: #672967; text-decoration: none }
12
td.postheader { font-family: sans-serif }
13
tr.address { font-family: sans-serif }
14
body { background: #ffffff; color: black; }</style>
7
<title>Qt 4.2: QSignalSpy Class Reference</title>
8
<link href="classic.css" rel="stylesheet" type="text/css" />
17
11
<table border="0" cellpadding="0" cellspacing="0" width="100%">
19
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
13
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
20
14
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="modules.html"><font color="#004faf">Modules</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
21
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">QSignalSpy Class Reference<br /><sup><sup>[<a href="qttest.html">QtTest</a> module]</sup></sup></h1>
15
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">QSignalSpy Class Reference<br /><sup><sup>[<a href="qttest.html">QtTest</a> module]</sup></sup></h1>
22
16
<p>The QSignalSpy class enables introspection of signal emission. <a href="#details">More...</a></p>
23
<pre>#include <QSignalSpy></pre><p>Inherits <a href="qobject.html">QObject</a> and <a href="qlist.html">QList<QList<QVariant> ></a>.</p>
17
<pre> #include <QSignalSpy></pre><p>Inherits <a href="qobject.html">QObject</a> and <a href="qlist.html">QList<QList<QVariant> ></a>.</p>
25
19
<li><a href="qsignalspy-members.html">List of all members, including inherited members</a></li>
50
44
<p>The QSignalSpy class enables introspection of signal emission.</p>
51
45
<p>QSignalSpy can connect to any signal of any object and records its emission. QSignalSpy itself is a list of <a href="qvariant.html">QVariant</a> lists. Each emission of the signal will append one item to the list, containing the arguments of the signal.</p>
52
46
<p>The following example records all signal emissions for the <tt>clicked()</tt> signal of a <a href="qcheckbox.html">QCheckBox</a>:</p>
53
<pre> QCheckBox *box = ...;
54
QSignalSpy spy(box, SIGNAL(clicked(bool)));
56
// do something that triggers the signal
57
box->animateClick();
59
COMPARE(spy.count(), 1); // make sure the signal was emitted exactly one time
60
QList<QVariant> arguments = spy.takeFirst(); // take the first signal
62
VERIFY(arguments.at(0).toBool() == true); // verify the first argument</pre>
47
<pre> QCheckBox *box = ...;
48
QSignalSpy spy(box, SIGNAL(clicked(bool)));
50
<span class="comment"> // do something that triggers the signal</span>
51
box->animateClick();
53
COMPARE(spy.count(), 1); <span class="comment">// make sure the signal was emitted exactly one time</span>
54
QList<QVariant> arguments = spy.takeFirst(); <span class="comment">// take the first signal</span>
56
VERIFY(arguments.at(0).toBool() == true); <span class="comment">// verify the first argument</span></pre>
63
57
<p><tt>spy.takeFirst()</tt> returns the arguments for the first emitted signal, as a list of <a href="qvariant.html">QVariant</a> objects. The <tt>clicked()</tt> signal has a single bool argument, which is stored as the first entry in the list of arguments.</p>
64
58
<p>The example below catches a signal from a custom object:</p>
65
<pre> QSignalSpy spy(myCustomObject, SIGNAL(mySignal(int, QString, double)));
67
myCustomObject->doSomething(); // trigger emission of the signal
69
QList<QVariant> arguments = spy.takeFirst();
70
VERIFY(arguments.at(0).type() == QVariant::Int);
71
VERIFY(arguments.at(1).type() == QVariant::QString);
72
VERIFY(arguments.at(2).type() == QVariant::double);</pre>
59
<pre> QSignalSpy spy(myCustomObject, SIGNAL(mySignal(int, QString, double)));
61
myCustomObject->doSomething(); <span class="comment">// trigger emission of the signal</span>
63
QList<QVariant> arguments = spy.takeFirst();
64
VERIFY(arguments.at(0).type() == QVariant::Int);
65
VERIFY(arguments.at(1).type() == QVariant::QString);
66
VERIFY(arguments.at(2).type() == QVariant::double);</pre>
73
67
<p><b>Note:</b> Non-standard data types need to be registered, using the <a href="qmetatype.html#qRegisterMetaType">qRegisterMetaType</a>() function, before you can create a QSignalSpy. For example:</p>
74
<pre> qRegisterMetaType<QModelIndex>("QModelIndex");
75
QSignalSpy spy(&model, SIGNAL(whatever(QModelIndex)));</pre>
68
<pre> qRegisterMetaType<QModelIndex>("QModelIndex");
69
QSignalSpy spy(&model, SIGNAL(whatever(QModelIndex)));</pre>
76
70
<p>To retrieve the <tt>QModelIndex</tt>, you can use qvariant_cast:</p>
77
<pre> // get the first argument from the first received signal:
78
QModelIndex result = qvariant_cast<QModelIndex>(spy.at(0).at(0));</pre>
71
<pre><span class="comment"> // get the first argument from the first received signal:</span>
72
QModelIndex result = qvariant_cast<QModelIndex>(spy.at(0).at(0));</pre>
80
74
<h2>Member Function Documentation</h2>
81
75
<h3 class="fn"><a name="QSignalSpy"></a>QSignalSpy::QSignalSpy ( <a href="qobject.html">QObject</a> * <i>object</i>, const char * <i>signal</i> )</h3>
82
76
<p>Constructs a new <a href="qsignalspy.html">QSignalSpy</a> that listens for emissions of the <i>signal</i> from the <a href="qobject.html">QObject</a> <i>object</i>. Neither <i>signal</i> nor <i>object</i> can be null.</p>
84
<pre> QSignalSpy spy(myPushButton, SIGNAL(clicked(bool)));</pre>
78
<pre> QSignalSpy spy(myPushButton, SIGNAL(clicked(bool)));</pre>
85
79
<h3 class="fn"><a name="isValid"></a>bool QSignalSpy::isValid () const</h3>
86
80
<p>Returns true if the signal spy listens to a valid signal, otherwise false.</p>
87
81
<h3 class="fn"><a name="signal"></a><a href="qbytearray.html">QByteArray</a> QSignalSpy::signal () const</h3>