Sophie

Sophie

distrib > Fedora > 18 > x86_64 > by-pkgid > ff187cb994c94c614ecc64c5a8528b1b > files > 7354

qt-doc-4.8.5-10.fc18.noarch.rpm

<?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" />
<!-- signalsandslots.qdoc -->
  <title>Qt 4.8: Signals &amp; Slots</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 &amp; 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>&nbsp;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 &amp; 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>Signals &amp;amp; Slots</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="#introduction">Introduction</a></li>
<li class="level1"><a href="#signals-and-slots">Signals and Slots</a></li>
<li class="level1"><a href="#a-small-example">A Small Example</a></li>
<li class="level1"><a href="#building-the-example">Building the Example</a></li>
<li class="level1"><a href="#signals">Signals</a></li>
<li class="level1"><a href="#slots">Slots</a></li>
<li class="level1"><a href="#meta-object-information">Meta-Object Information</a></li>
<li class="level1"><a href="#a-real-example">A Real Example</a></li>
<li class="level1"><a href="#signals-and-slots-with-default-arguments">Signals And Slots With Default Arguments</a></li>
<li class="level1"><a href="#advanced-signals-and-slots-usage">Advanced Signals and Slots Usage</a></li>
<li class="level2"><a href="#using-qt-with-3rd-party-signals-and-slots">Using Qt with 3rd Party Signals and Slots</a></li>
</ul>
</div>
<h1 class="title">Signals &amp; Slots</h1>
<span class="subtitle"></span>
<!-- $$$signalsandslots.html-description -->
<div class="descr"> <a name="details"></a>
<p>Signals and slots are used for communication between objects. The signals and slots mechanism is a central feature of Qt and probably the part that differs most from the features provided by other frameworks.</p>
<a name="introduction"></a>
<h2>Introduction</h2>
<p>In GUI programming, when we change one widget, we often want another widget to be notified. More generally, we want objects of any kind to be able to communicate with one another. For example, if a user clicks a <b>Close</b> button, we probably want the window's <a href="qwidget.html#close">close()</a> function to be called.</p>
<p>Older toolkits achieve this kind of communication using callbacks. A callback is a pointer to a function, so if you want a processing function to notify you about some event you pass a pointer to another function (the callback) to the processing function. The processing function then calls the callback when appropriate. Callbacks have two fundamental flaws: Firstly, they are not type-safe. We can never be certain that the processing function will call the callback with the correct arguments. Secondly, the callback is strongly coupled to the processing function since the processing function must know which callback to call.</p>
<a name="signals-and-slots"></a>
<h2>Signals and Slots</h2>
<p>In Qt, we have an alternative to the callback technique: We use signals and slots. A signal is emitted when a particular event occurs. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal. Qt's widgets have many pre-defined slots, but it is common practice to subclass widgets and add your own slots so that you can handle the signals that you are interested in.</p>
<p class="centerAlign"><img src="images/abstract-connections.png" alt="" /></p><p>The signals and slots mechanism is type safe: The signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.) Since the signatures are compatible, the compiler can help us detect type mismatches. Signals and slots are loosely coupled: A class which emits a signal neither knows nor cares which slots receive the signal. Qt's signals and slots mechanism ensures that if you connect a signal to a slot, the slot will be called with the signal's parameters at the right time. Signals and slots can take any number of arguments of any type. They are completely type safe.</p>
<p>All classes that inherit from <a href="qobject.html">QObject</a> or one of its subclasses (e.g&#x2e;, <a href="qwidget.html">QWidget</a>) can contain signals and slots. Signals are emitted by objects when they change their state in a way that may be interesting to other objects. This is all the object does to communicate. It does not know or care whether anything is receiving the signals it emits. This is true information encapsulation, and ensures that the object can be used as a software component.</p>
<p>Slots can be used for receiving signals, but they are also normal member functions. Just as an object does not know if anything receives its signals, a slot does not know if it has any signals connected to it. This ensures that truly independent components can be created with Qt.</p>
<p>You can connect as many signals as you want to a single slot, and a signal can be connected to as many slots as you need. It is even possible to connect a signal directly to another signal. (This will emit the second signal immediately whenever the first is emitted.)</p>
<p>Together, signals and slots make up a powerful component programming mechanism.</p>
<a name="a-small-example"></a>
<h2>A Small Example</h2>
<p>A minimal C++ class declaration might read:</p>
<pre class="cpp"> <span class="keyword">class</span> Counter
 {
 <span class="keyword">public</span>:
     Counter() { m_value <span class="operator">=</span> <span class="number">0</span>; }

     <span class="type">int</span> value() <span class="keyword">const</span> { <span class="keyword">return</span> m_value; }
     <span class="type">void</span> setValue(<span class="type">int</span> value);

 <span class="keyword">private</span>:
     <span class="type">int</span> m_value;
 };</pre>
<p>A small <a href="qobject.html">QObject</a>-based class might read:</p>
<pre class="cpp"> <span class="preprocessor">#include &lt;QObject&gt;</span>

 <span class="keyword">class</span> Counter : <span class="keyword">public</span> <span class="type"><a href="qobject.html">QObject</a></span>
 {
     Q_OBJECT

 <span class="keyword">public</span>:
     Counter() { m_value <span class="operator">=</span> <span class="number">0</span>; }

     <span class="type">int</span> value() <span class="keyword">const</span> { <span class="keyword">return</span> m_value; }

 <span class="keyword">public</span> <span class="keyword">slots</span>:
     <span class="type">void</span> setValue(<span class="type">int</span> value);

 <span class="keyword">signals</span>:
     <span class="type">void</span> valueChanged(<span class="type">int</span> newValue);

 <span class="keyword">private</span>:
     <span class="type">int</span> m_value;
 };</pre>
<p>The <a href="qobject.html">QObject</a>-based version has the same internal state, and provides public methods to access the state, but in addition it has support for component programming using signals and slots. This class can tell the outside world that its state has changed by emitting a signal, <tt>valueChanged()</tt>, and it has a slot which other objects can send signals to.</p>
<p>All classes that contain signals or slots must mention <a href="qobject.html#Q_OBJECT">Q_OBJECT</a> at the top of their declaration. They must also derive (directly or indirectly) from <a href="qobject.html">QObject</a>.</p>
<p>Slots are implemented by the application programmer. Here is a possible implementation of the <tt>Counter::setValue()</tt> slot:</p>
<pre class="cpp"> <span class="type">void</span> Counter<span class="operator">::</span>setValue(<span class="type">int</span> value)
 {
     <span class="keyword">if</span> (value <span class="operator">!</span><span class="operator">=</span> m_value) {
         m_value <span class="operator">=</span> value;
         <span class="keyword">emit</span> valueChanged(value);
     }
 }</pre>
<p>The <tt>emit</tt> line emits the signal <tt>valueChanged()</tt> from the object, with the new value as argument.</p>
<p>In the following code snippet, we create two <tt>Counter</tt> objects and connect the first object's <tt>valueChanged()</tt> signal to the second object's <tt>setValue()</tt> slot using <a href="qobject.html#connect">QObject::connect</a>():</p>
<pre class="cpp">     Counter a<span class="operator">,</span> b;
     <span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>connect(<span class="operator">&amp;</span>a<span class="operator">,</span> SIGNAL(valueChanged(<span class="type">int</span>))<span class="operator">,</span>
                      <span class="operator">&amp;</span>b<span class="operator">,</span> SLOT(setValue(<span class="type">int</span>)));

     a<span class="operator">.</span>setValue(<span class="number">12</span>);     <span class="comment">// a.value() == 12, b.value() == 12</span>
     b<span class="operator">.</span>setValue(<span class="number">48</span>);     <span class="comment">// a.value() == 12, b.value() == 48</span></pre>
<p>Calling <tt>a.setValue(12)</tt> makes <tt>a</tt> emit a <tt>valueChanged(12)</tt> signal, which <tt>b</tt> will receive in its <tt>setValue()</tt> slot, i.e&#x2e; <tt>b.setValue(12)</tt> is called. Then <tt>b</tt> emits the same <tt>valueChanged()</tt> signal, but since no slot has been connected to <tt>b</tt>'s <tt>valueChanged()</tt> signal, the signal is ignored.</p>
<p>Note that the <tt>setValue()</tt> function sets the value and emits the signal only if <tt>value != m_value</tt>. This prevents infinite looping in the case of cyclic connections (e.g&#x2e;, if <tt>b.valueChanged()</tt> were connected to <tt>a.setValue()</tt>).</p>
<p>By default, for every connection you make, a signal is emitted; two signals are emitted for duplicate connections. You can break all of these connections with a single disconnect() call. If you pass the <a href="qt.html#ConnectionType-enum">Qt::UniqueConnection</a> <i>type</i>, the connection will only be made if it is not a duplicate. If there is already a duplicate (exact same signal to the exact same slot on the same objects), the connection will fail and connect will return false</p>
<p>This example illustrates that objects can work together without needing to know any information about each other. To enable this, the objects only need to be connected together, and this can be achieved with some simple <a href="qobject.html#connect">QObject::connect</a>() function calls, or with <tt>uic</tt>'s <a href="designer-using-a-ui-file.html#automatic-connections">automatic connections</a> feature.</p>
<a name="building-the-example"></a>
<h2>Building the Example</h2>
<p>The C++ preprocessor changes or removes the <tt>signals</tt>, <tt>slots</tt>, and <tt>emit</tt> keywords so that the compiler is presented with standard C++.</p>
<p>By running the <a href="moc.html#moc">moc</a> on class definitions that contain signals or slots, a C++ source file is produced which should be compiled and linked with the other object files for the application. If you use <a href="qmake-manual.html#qmake">qmake</a>, the makefile rules to automatically invoke <tt>moc</tt> will be added to your project's makefile.</p>
<a name="signals"></a>
<h2>Signals</h2>
<p>Signals are emitted by an object when its internal state has changed in some way that might be interesting to the object's client or owner. Only the class that defines a signal and its subclasses can emit the signal.</p>
<p>When a signal is emitted, the slots connected to it are usually executed immediately, just like a normal function call. When this happens, the signals and slots mechanism is totally independent of any GUI event loop. Execution of the code following the <tt>emit</tt> statement will occur once all slots have returned. The situation is slightly different when using <a href="qt.html#ConnectionType-enum">queued connections</a>; in such a case, the code following the <tt>emit</tt> keyword will continue immediately, and the slots will be executed later.</p>
<p>If several slots are connected to one signal, the slots will be executed one after the other, in the order they have been connected, when the signal is emitted.</p>
<p>Signals are automatically generated by the <a href="moc.html#moc">moc</a> and must not be implemented in the <tt>.cpp</tt> file. They can never have return types (i.e&#x2e; use <tt>void</tt>).</p>
<p>A note about arguments: Our experience shows that signals and slots are more reusable if they do not use special types. If <a href="qabstractslider.html#valueChanged">QScrollBar::valueChanged</a>() were to use a special type such as the hypothetical QScrollBar::Range, it could only be connected to slots designed specifically for <a href="qscrollbar.html">QScrollBar</a>. Connecting different input widgets together would be impossible.</p>
<a name="slots"></a>
<h2>Slots</h2>
<p>A slot is called when a signal connected to it is emitted. Slots are normal C++ functions and can be called normally; their only special feature is that signals can be connected to them.</p>
<p>Since slots are normal member functions, they follow the normal C++ rules when called directly. However, as slots, they can be invoked by any component, regardless of its access level, via a signal-slot connection. This means that a signal emitted from an instance of an arbitrary class can cause a private slot to be invoked in an instance of an unrelated class.</p>
<p>You can also define slots to be virtual, which we have found quite useful in practice.</p>
<p>Compared to callbacks, signals and slots are slightly slower because of the increased flexibility they provide, although the difference for real applications is insignificant. In general, emitting a signal that is connected to some slots, is approximately ten times slower than calling the receivers directly, with non-virtual function calls. This is the overhead required to locate the connection object, to safely iterate over all connections (i.e&#x2e; checking that subsequent receivers have not been destroyed during the emission), and to marshall any parameters in a generic fashion. While ten non-virtual function calls may sound like a lot, it's much less overhead than any <tt>new</tt> or <tt>delete</tt> operation, for example. As soon as you perform a string, vector or list operation that behind the scene requires <tt>new</tt> or <tt>delete</tt>, the signals and slots overhead is only responsible for a very small proportion of the complete function call costs.</p>
<p>The same is true whenever you do a system call in a slot; or indirectly call more than ten functions. On an i586-500, you can emit around 2,000,000 signals per second connected to one receiver, or around 1,200,000 per second connected to two receivers. The simplicity and flexibility of the signals and slots mechanism is well worth the overhead, which your users won't even notice.</p>
<p>Note that other libraries that define variables called <tt>signals</tt> or <tt>slots</tt> may cause compiler warnings and errors when compiled alongside a Qt-based application. To solve this problem, <tt>#undef</tt> the offending preprocessor symbol.</p>
<a name="meta-object-information"></a>
<h2>Meta-Object Information</h2>
<p>The meta-object compiler (<a href="moc.html#moc">moc</a>) parses the class declaration in a C++ file and generates C++ code that initializes the meta-object. The meta-object contains the names of all the signal and slot members, as well as pointers to these functions.</p>
<p>The meta-object contains additional information such as the object's <a href="qobject-qt3.html#className" class="compat">class name</a>. You can also check if an object <a href="qobject.html#inherits">inherits</a> a specific class, for example:</p>
<pre class="cpp">     <span class="keyword">if</span> (widget<span class="operator">-</span><span class="operator">&gt;</span>inherits(<span class="string">&quot;QAbstractButton&quot;</span>)) {
         <span class="type"><a href="qabstractbutton.html">QAbstractButton</a></span> <span class="operator">*</span>button <span class="operator">=</span> <span class="keyword">static_cast</span><span class="operator">&lt;</span><span class="type"><a href="qabstractbutton.html">QAbstractButton</a></span> <span class="operator">*</span><span class="operator">&gt;</span>(widget);
         button<span class="operator">-</span><span class="operator">&gt;</span>toggle();
     }</pre>
<p>The meta-object information is also used by qobject_cast&lt;T&gt;(), which is similar to <a href="qobject.html#inherits">QObject::inherits</a>() but is less error-prone:</p>
<pre class="cpp">     <span class="keyword">if</span> (<span class="type"><a href="qabstractbutton.html">QAbstractButton</a></span> <span class="operator">*</span>button <span class="operator">=</span> qobject_cast<span class="operator">&lt;</span><span class="type"><a href="qabstractbutton.html">QAbstractButton</a></span> <span class="operator">*</span><span class="operator">&gt;</span>(widget))
         button<span class="operator">-</span><span class="operator">&gt;</span>toggle();</pre>
<p>See <a href="metaobjects.html#meta-object-system">Meta-Object System</a> for more information.</p>
<a name="a-real-example"></a>
<h2>A Real Example</h2>
<p>Here is a simple commented example of a widget.</p>
<pre class="cpp"> <span class="preprocessor">#ifndef LCDNUMBER_H</span>
 <span class="preprocessor">#define LCDNUMBER_H</span>

 <span class="preprocessor">#include &lt;QFrame&gt;</span>

 <span class="keyword">class</span> LcdNumber : <span class="keyword">public</span> <span class="type"><a href="qframe.html">QFrame</a></span>
 {
     Q_OBJECT</pre>
<p><tt>LcdNumber</tt> inherits <a href="qobject.html">QObject</a>, which has most of the signal-slot knowledge, via <a href="qframe.html">QFrame</a> and <a href="qwidget.html">QWidget</a>. It is somewhat similar to the built-in <a href="qlcdnumber.html">QLCDNumber</a> widget.</p>
<p>The <a href="qobject.html#Q_OBJECT">Q_OBJECT</a> macro is expanded by the preprocessor to declare several member functions that are implemented by the <tt>moc</tt>; if you get compiler errors along the lines of &quot;undefined reference to vtable for <tt>LcdNumber</tt>&quot;, you have probably forgotten to <a href="moc.html#moc">run the moc</a> or to include the moc output in the link command.</p>
<pre class="cpp"> <span class="keyword">public</span>:
     LcdNumber(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);</pre>
<p>It's not obviously relevant to the moc, but if you inherit <a href="qwidget.html">QWidget</a> you almost certainly want to have the <tt>parent</tt> argument in your constructor and pass it to the base class's constructor.</p>
<p>Some destructors and member functions are omitted here; the <tt>moc</tt> ignores member functions.</p>
<pre class="cpp"> <span class="keyword">signals</span>:
     <span class="type">void</span> overflow();</pre>
<p><tt>LcdNumber</tt> emits a signal when it is asked to show an impossible value.</p>
<p>If you don't care about overflow, or you know that overflow cannot occur, you can ignore the <tt>overflow()</tt> signal, i.e&#x2e; don't connect it to any slot.</p>
<p>If on the other hand you want to call two different error functions when the number overflows, simply connect the signal to two different slots. Qt will call both (in the order they were connected).</p>
<pre class="cpp"> <span class="keyword">public</span> <span class="keyword">slots</span>:
     <span class="type">void</span> display(<span class="type">int</span> num);
     <span class="type">void</span> display(<span class="type">double</span> num);
     <span class="type">void</span> display(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>str);
     <span class="type">void</span> setHexMode();
     <span class="type">void</span> setDecMode();
     <span class="type">void</span> setOctMode();
     <span class="type">void</span> setBinMode();
     <span class="type">void</span> setSmallDecimalPoint(<span class="type">bool</span> point);
 };

 <span class="preprocessor">#endif</span></pre>
<p>A slot is a receiving function used to get information about state changes in other widgets. <tt>LcdNumber</tt> uses it, as the code above indicates, to set the displayed number. Since <tt>display()</tt> is part of the class's interface with the rest of the program, the slot is public.</p>
<p>Several of the example programs connect the <a href="qabstractslider.html#valueChanged">valueChanged()</a> signal of a <a href="qscrollbar.html">QScrollBar</a> to the <tt>display()</tt> slot, so the LCD number continuously shows the value of the scroll bar.</p>
<p>Note that <tt>display()</tt> is overloaded; Qt will select the appropriate version when you connect a signal to the slot. With callbacks, you'd have to find five different names and keep track of the types yourself.</p>
<p>Some irrelevant member functions have been omitted from this example.</p>
<a name="signals-and-slots-with-default-arguments"></a>
<h2>Signals And Slots With Default Arguments</h2>
<p>The signatures of signals and slots may contain arguments, and the arguments can have default values. Consider <a href="qobject.html#destroyed">QObject::destroyed</a>():</p>
<pre class="cpp"> <span class="type">void</span> destroyed(<span class="type"><a href="qobject.html">QObject</a></span><span class="operator">*</span> <span class="operator">=</span> <span class="number">0</span>);</pre>
<p>When a <a href="qobject.html">QObject</a> is deleted, it emits this <a href="qobject.html#destroyed">QObject::destroyed</a>() signal. We want to catch this signal, wherever we might have a dangling reference to the deleted <a href="qobject.html">QObject</a>, so we can clean it up. A suitable slot signature might be:</p>
<pre class="cpp"> <span class="type">void</span> objectDestroyed(<span class="type"><a href="qobject.html">QObject</a></span><span class="operator">*</span> obj <span class="operator">=</span> <span class="number">0</span>);</pre>
<p>To connect the signal to the slot, we use <a href="qobject.html#connect">QObject::connect</a>() and the <tt>SIGNAL()</tt> and <tt>SLOT()</tt> macros. The rule about whether to include arguments or not in the <tt>SIGNAL()</tt> and <tt>SLOT()</tt> macros, if the arguments have default values, is that the signature passed to the <tt>SIGNAL()</tt> macro must <i>not</i> have fewer arguments than the signature passed to the <tt>SLOT()</tt> macro.</p>
<p>All of these would work:</p>
<pre class="cpp"> connect(sender<span class="operator">,</span> SIGNAL(destroyed(<span class="type"><a href="qobject.html">QObject</a></span><span class="operator">*</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(objectDestroyed(Qbject<span class="operator">*</span>)));
 connect(sender<span class="operator">,</span> SIGNAL(destroyed(<span class="type"><a href="qobject.html">QObject</a></span><span class="operator">*</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(objectDestroyed()));
 connect(sender<span class="operator">,</span> SIGNAL(destroyed())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(objectDestroyed()));</pre>
<p>But this one won't work:</p>
<pre class="cpp"> connect(sender<span class="operator">,</span> SIGNAL(destroyed())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(objectDestroyed(<span class="type"><a href="qobject.html">QObject</a></span><span class="operator">*</span>)));</pre>
<p>...because the slot will be expecting a <a href="qobject.html">QObject</a> that the signal will not send. This connection will report a runtime error.</p>
<a name="advanced-signals-and-slots-usage"></a>
<h2>Advanced Signals and Slots Usage</h2>
<p>For cases where you may require information on the sender of the signal, Qt provides the <a href="qobject.html#sender">QObject::sender</a>() function, which returns a pointer to the object that sent the signal.</p>
<p>The <a href="qsignalmapper.html">QSignalMapper</a> class is provided for situations where many signals are connected to the same slot and the slot needs to handle each signal differently.</p>
<p>Suppose you have three push buttons that determine which file you will open: &quot;Tax File&quot;, &quot;Accounts File&quot;, or &quot;Report File&quot;.</p>
<p>In order to open the correct file, you use <a href="qsignalmapper.html#setMapping">QSignalMapper::setMapping</a>() to map all the clicked() signals to a <a href="qsignalmapper.html">QSignalMapper</a> object. Then you connect the file's <a href="qabstractbutton.html#clicked">QPushButton::clicked</a>() signal to the <a href="qsignalmapper.html#map">QSignalMapper::map</a>() slot.</p>
<pre class="cpp">     signalMapper <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qsignalmapper.html">QSignalMapper</a></span>(<span class="keyword">this</span>);
     signalMapper<span class="operator">-</span><span class="operator">&gt;</span>setMapping(taxFileButton<span class="operator">,</span> <span class="type"><a href="qstring.html">QString</a></span>(<span class="string">&quot;taxfile.txt&quot;</span>));
     signalMapper<span class="operator">-</span><span class="operator">&gt;</span>setMapping(accountFileButton<span class="operator">,</span> <span class="type"><a href="qstring.html">QString</a></span>(<span class="string">&quot;accountsfile.txt&quot;</span>));
     signalMapper<span class="operator">-</span><span class="operator">&gt;</span>setMapping(reportFileButton<span class="operator">,</span> <span class="type"><a href="qstring.html">QString</a></span>(<span class="string">&quot;reportfile.txt&quot;</span>));

     connect(taxFileButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span>
         signalMapper<span class="operator">,</span> SLOT (map()));
     connect(accountFileButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span>
         signalMapper<span class="operator">,</span> SLOT (map()));
     connect(reportFileButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span>
         signalMapper<span class="operator">,</span> SLOT (map()));</pre>
<p>Then, you connect the <a href="qsignalmapper.html#mapped">mapped()</a> signal to <tt>readFile()</tt> where a different file will be opened, depending on which push button is pressed.</p>
<pre class="cpp">     connect(signalMapper<span class="operator">,</span> SIGNAL(mapped(<span class="type"><a href="qstring.html">QString</a></span>))<span class="operator">,</span>
         <span class="keyword">this</span><span class="operator">,</span> SLOT(readFile(<span class="type"><a href="qstring.html">QString</a></span>)));</pre>
<p><b>Note:</b> The following code will compile and run, but due to signature normalization, the code will be slower.</p>
<pre class="cpp">     //slower due to signature normalization at runtime

     connect(signalMapper, SIGNAL(mapped(const QString &amp;)),
         this, SLOT(readFile(const QString &amp;)));</pre>
<a name="3rd-party-signals-and-slots"></a><a name="using-qt-with-3rd-party-signals-and-slots"></a>
<h3>Using Qt with 3rd Party Signals and Slots</h3>
<p>It is possible to use Qt with a 3rd party signal/slot mechanism. You can even use both mechanisms in the same project. Just add the following line to your qmake project (.pro) file.</p>
<pre class="cpp"> CONFIG <span class="operator">+</span><span class="operator">=</span> no_keywords</pre>
<p>It tells Qt not to define the moc keywords <tt>signals</tt>, <tt>slots</tt>, and <tt>emit</tt>, because these names will be used by a 3rd party library, e.g&#x2e; Boost. Then to continue using Qt signals and slots with the <tt>no_keywords</tt> flag, simply replace all uses of the Qt moc keywords in your sources with the corresponding Qt macros <a href="qobject.html#Q_SIGNALS">Q_SIGNALS</a> (or <a href="qobject.html#Q_SIGNAL">Q_SIGNAL</a>), <a href="qobject.html#Q_SLOTS">Q_SLOTS</a> (or <a href="qobject.html#Q_SLOT">Q_SLOT</a>), and <a href="qobject.html#Q_EMIT">Q_EMIT</a>.</p>
</div>
<p><b>See also </b><a href="metaobjects.html#meta-object-system">Meta-Object System</a> and <a href="properties.html#qt-s-property-system">Qt's Property System</a>.</p>
<!-- @@@signalsandslots.html -->
      </div>
    </div>
    </div> 
    <div class="ft">
      <span></span>
    </div>
  </div> 
  <div class="footer">
    <p>
      <acronym title="Copyright">&copy;</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>