title: How to pass a QString to a Qt slot from a QMenu via QSignalMapper or otherwise
body: <p>I have a QMenu with many submenus. These are dynamically created i.e. the names menus come from a db and created in a loop. Now i wanted to fire the same slot triggered() or similar when a menu is clicked, but i needed the QString menu name to be passed to slot so i could perform menu specific actions. I have tried <a href="https://stackoverflow.com/questions/9187538/qt-how-to-add-a-list-of-qactions-to-qmenu-and-handle-them-with-a-single-slot">this</a> i.e. passing a QAction * to the triggered event and used setData but i am getting the run time error.</p>
  
  <blockquote>
    <p>object::connect: No such signal QAction::triggered(QAction *)</p>
  </blockquote>
  
  <pre><code>for(int j=0; j&lt;channelTypes[i].getNumChannels() ; j++){
          QAction *subMenuAct = subMenu-&gt;addAction(tr(c_name)); // c_name the menu name
          subMenuAct-&gt;setData(ch_name);
          connect(subMenuAct, SIGNAL(triggered(QAction *)), this, SLOT(playChannel(QAction *))); // playChannel is the slot
  }
  
  void &lt;ClassName&gt;::playChannel(QAction *channelAction)
  {
       QString str = channelAction-&gt;data().toString();
      qDebug() &lt;&lt; "Selected - " &lt;&lt; str;
  }
  </code></pre>
  
  <p>Alternately, i have also tried QSignalMapper where signalMapper is a data member initialized in the constructor</p>
  
  <pre><code>signalMapper = new QSignalMapper(this);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>for(int j=0; j&lt;channelTypes[i].getNumChannels() ; j++){
         QAction *subMenuAct = subMenu-&gt;addAction(tr(c_name));
  
         connect(subMenuAct, SIGNAL(triggered()), signalMapper, SLOT(map()));
         signalMapper-&gt;setMapping(subMenu, ch_name);
         connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(playChannel(QString)));
  }
  </code></pre>
  
  <p>In the second case, i don't get any error, however the slot function playChannel is not being called. Would really appreciate if some one could help resolving it.</p>
  
  <p><strong>Update 1:</strong> The only difference that i see from other examples i have seen is that usually people are <em>connecting signals from several widgets to a single slot</em> (say different buttons). In my case i am connecting <em>several sub menus (with different names) to a single slot</em>. Should this make any difference?</p>
  
  <p><strong>Update 2:</strong> It worked after the correction suggested in the solution <a href="https://stackoverflow.com/a/14157471/986760">below</a> for QSignalMapper. Also the fact that i was using SubMenu as argument to setMapping , where as MenuAction item should have been used instead. But now i am getting event fired multiple times i.e. as many times as there are entries in the main menu for the selected sub menu category. If channel type is English (main menu) with four entries), HBO, star movies etc. (sub menu), and i choose HBO, then event is fired four times with string HBO. It works fine if i create a separate signal mapper for each submenu. But i was hoping a single mapper should be used and i am doing something incorrectly here. Some more details in the comments to the answer.</p>