Sophie

Sophie

distrib > Mageia > 6 > armv5tl > media > core-updates > by-pkgid > 768f7d9f703884aa2562bf0a651086df > files > 887

qtbase5-doc-5.9.4-1.1.mga6.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- signalsandslots.qdoc -->
  <title>Signals &amp; Slots | Qt Core 5.9</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td >Qt 5.9</td><td ><a href="qtcore-index.html">Qt Core</a></td><td >Signals &amp; Slots</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<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="#signals">Signals</a></li>
<li class="level1"><a href="#slots">Slots</a></li>
<li class="level1"><a href="#a-small-example">A Small Example</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>
<div class="sidebar-content" id="sidebar-content"></div></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. Signals and slots are made possible by Qt's <a href="metaobjects.html">meta-object system</a>.</p>
<a name="introduction"></a>
<h2 id="introduction">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="../qtwidgets/qwidget.html#close">close()</a> function to be called.</p>
<p>Other 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. While successful frameworks using this method do exist, callbacks can be unintuitive and may suffer from problems in ensuring the type-correctness of callback arguments.</p>
<a name="signals-and-slots"></a>
<h2 id="signals-and-slots">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 when using the function pointer-based syntax. The string-based SIGNAL and SLOT syntax will detect type mismatches at runtime. 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="../qtwidgets/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="signals"></a>
<h2 id="signals">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. Signals are public access functions and can be emitted from anywhere, but we recommend to only emit them from the class that defines the signal and its subclasses.</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 <code>emit</code> 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 <code>emit</code> 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 moc and must not be implemented in the <code>.cpp</code> file. They can never have return types (i.e&#x2e; use <code>void</code>).</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="../qtwidgets/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="../qtwidgets/qscrollbar.html">QScrollBar</a>. Connecting different input widgets together would be impossible.</p>
<a name="slots"></a>
<h2 id="slots">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 <code>new</code> or <code>delete</code> operation, for example. As soon as you perform a string, vector or list operation that behind the scene requires <code>new</code> or <code>delete</code>, the signals and slots overhead is only responsible for a very small proportion of the complete function call costs. The same is true whenever you do a system call in a slot; or indirectly call more than ten functions. 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 <code>signals</code> or <code>slots</code> may cause compiler warnings and errors when compiled alongside a Qt-based application. To solve this problem, <code>#undef</code> the offending preprocessor symbol.</p>
<a name="a-small-example"></a>
<h2 id="a-small-example">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, <code>valueChanged()</code>, 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 <code>Counter::setValue()</code> 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 <code>emit</code> line emits the signal <code>valueChanged()</code> from the object, with the new value as argument.</p>
<p>In the following code snippet, we create two <code>Counter</code> objects and connect the first object's <code>valueChanged()</code> signal to the second object's <code>setValue()</code> 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> <span class="operator">&amp;</span>Counter<span class="operator">::</span>valueChanged<span class="operator">,</span>
                       <span class="operator">&amp;</span>b<span class="operator">,</span> <span class="operator">&amp;</span>Counter<span class="operator">::</span>setValue);

      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 <code>a.setValue(12)</code> makes <code>a</code> emit a <code>valueChanged(12)</code> signal, which <code>b</code> will receive in its <code>setValue()</code> slot, i.e&#x2e; <code>b.setValue(12)</code> is called. Then <code>b</code> emits the same <code>valueChanged()</code> signal, but since no slot has been connected to <code>b</code>'s <code>valueChanged()</code> signal, the signal is ignored.</p>
<p>Note that the <code>setValue()</code> function sets the value and emits the signal only if <code>value != m_value</code>. This prevents infinite looping in the case of cyclic connections (e.g&#x2e;, if <code>b.valueChanged()</code> were connected to <code>a.setValue()</code>).</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 <a href="qobject.html#disconnect">disconnect()</a> 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 <code>uic</code>'s automatic connections feature.</p>
<a name="a-real-example"></a>
<h2 id="a-real-example">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="../qtwidgets/qframe.html">QFrame</a></span>
  {
      Q_OBJECT

</pre>
<p><code>LcdNumber</code> inherits <a href="qobject.html">QObject</a>, which has most of the signal-slot knowledge, via <a href="../qtwidgets/qframe.html">QFrame</a> and <a href="../qtwidgets/qwidget.html">QWidget</a>. It is somewhat similar to the built-in <a href="../qtwidgets/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 <code>moc</code>; if you get compiler errors along the lines of &quot;undefined reference to vtable for <code>LcdNumber</code>&quot;, you have probably forgotten to run the moc 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="../qtwidgets/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="../qtwidgets/qwidget.html">QWidget</a> you almost certainly want to have the <code>parent</code> argument in your constructor and pass it to the base class's constructor.</p>
<p>Some destructors and member functions are omitted here; the <code>moc</code> ignores member functions.</p>
<pre class="cpp">

  <span class="keyword">signals</span>:
      <span class="type">void</span> overflow();

</pre>
<p><code>LcdNumber</code> 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 <code>overflow()</code> 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(bool point);
  };

  <span class="preprocessor">#endif</span>

</pre>
<p>A slot is a receiving function used to get information about state changes in other widgets. <code>LcdNumber</code> uses it, as the code above indicates, to set the displayed number. Since <code>display()</code> 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="../qtwidgets/qabstractslider.html#valueChanged">valueChanged()</a> signal of a <a href="../qtwidgets/qscrollbar.html">QScrollBar</a> to the <code>display()</code> slot, so the LCD number continuously shows the value of the scroll bar.</p>
<p>Note that <code>display()</code> 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 id="signals-and-slots-with-default-arguments">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>(). There are several ways to connect signal and slots. The first is to use function pointers:</p>
<pre class="cpp">

  connect(sender<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>destroyed<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MyObject<span class="operator">::</span>objectDestroyed);

</pre>
<p>There are several advantages to using <a href="qobject.html#connect">QObject::connect</a>() with function pointers. First, it allows the compiler to check that the signal's arguments are compatible with the slot's arguments. Arguments can also be implicitly converted by the compiler, if needed.</p>
<p>You can also connect to functors or C++11 lambdas:</p>
<pre class="cpp">

  connect(sender<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qobject.html">QObject</a></span><span class="operator">::</span>destroyed<span class="operator">,</span> <span class="operator">[</span><span class="operator">=</span><span class="operator">]</span>(){ <span class="keyword">this</span><span class="operator">-</span><span class="operator">&gt;</span>m_objects<span class="operator">.</span>remove(sender); });

</pre>
<p>The other way to connect a signal to a slot is to use <a href="qobject.html#connect">QObject::connect</a>() and the <code>SIGNAL</code> and <code>SLOT</code> macros. The rule about whether to include arguments or not in the <code>SIGNAL()</code> and <code>SLOT()</code> macros, if the arguments have default values, is that the signature passed to the <code>SIGNAL()</code> macro must <i>not</i> have fewer arguments than the signature passed to the <code>SLOT()</code> 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>
<p>Note that signal and slot arguments are not checked by the compiler when using this <a href="qobject.html#connect">QObject::connect</a>() overload.</p>
<a name="advanced-signals-and-slots-usage"></a>
<h2 id="advanced-signals-and-slots-usage">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 <a href="../qtwidgets/qabstractbutton.html#clicked">QPushButton::clicked</a>() signals to a <a href="qsignalmapper.html">QSignalMapper</a> object. Then you connect the file's <a href="../qtwidgets/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> <span class="operator">&amp;</span><span class="type"><a href="../qtwidgets/qpushbutton.html">QPushButton</a></span><span class="operator">::</span>clicked<span class="operator">,</span>
          signalMapper<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qsignalmapper.html">QSignalMapper</a></span><span class="operator">::</span>map);
      connect(accountFileButton<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="../qtwidgets/qpushbutton.html">QPushButton</a></span><span class="operator">::</span>clicked<span class="operator">,</span>
          signalMapper<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qsignalmapper.html">QSignalMapper</a></span><span class="operator">::</span>map);
      connect(reportFileButton<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="../qtwidgets/qpushbutton.html">QPushButton</a></span><span class="operator">::</span>clicked<span class="operator">,</span>
          signalMapper<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qsignalmapper.html">QSignalMapper</a></span><span class="operator">::</span>map);

</pre>
<p>Then, you connect the <a href="qsignalmapper.html#mapped">mapped()</a> signal to <code>readFile()</code> 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>
<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 <code>signals</code>, <code>slots</code>, and <code>emit</code>, 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 <code>no_keywords</code> 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</a> and <a href="properties.html">Qt's Property System</a>.</p>
<!-- @@@signalsandslots.html -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2017 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br>    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.<br>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>