title: Qt: dynamic widgets signal and slot connection
body: <p>In my Qt app, I create some of my widgets dynamically. Among them are QToolButtons that need to have a similar behavior.</p>
  
  <p>Here is how the widgets are created:</p>
  
  <pre><code>QMap&lt;QString, QToolButton*&gt; unselectButtonMap;
  foreach(QString instance, ...) {
    unselectButtonMap[instance] = new QToolButton;
    myLayout-&gt;addWidget(unselectButtonMap[instance]);
    QObject::connect(unselectButtonMap[instance],SIGNAL(clicked()),this,SLOT(unselectInstance()));
  }
  </code></pre>
  
  <p>Now I would like the <code>unselectInstence</code> slot to know which instance is concerned.</p>
  
  <p>I first thougth about giving the instance name as a parameter to the slot, but slots only take parameters from signals. The idea was something like:</p>
  
  <pre><code>QObject::connect(unselectButtonMap[instance],SIGNAL(clicked()),this,SLOT(unselectInstance(instance)));
  </code></pre>
  
  <p>Any other idea on how to do that ?</p>
  
  <p>Notes: I'm using Qt4.8</p>
  
  <p><strong>EDIT:</strong> based on <strong>shan</strong>'s answer, and because I needed the instance name instead of the QToolButton itself, here is what I came up to</p>
  
  <pre><code>void MyWindow::unselectInstance() {
    foreach(QString instance, unselectButtonMap.keys()) {
      if(unselectButtonMap[instance] == QObject::sender()) {
        //do stuff here...
      }
    }
  }
  </code></pre>
  
  <p>The pointer comparison seems to work pretty well.</p>
  
  <p><strong>RE-EDIT:</strong> and based on <strong>user1313312</strong>'s answer, we would declare a QSignalMapper:</p>
  
  <pre><code>QSignalMapper *signalMapper = new QSignalMapper(this);
  QObject::connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(unselectInstance(QString)));
  </code></pre>
  
  <p>then connect the QToolButton to the mapper</p>
  
  <pre><code>signalMapper-&gt;setMapping(unselectButtonMap[instance], instance);
  QObject::connect(unselectButtonMap[instance],SIGNAL(clicked()),signalMapper,SLOT(map()));
  </code></pre>
  
  <p>and the slot would be way much simpler:</p>
  
  <pre><code>void MyWindow::unselectinstance(QString instance) {
    //do stuff here...
  }
  </code></pre>