[Qt-interest] Help on QAction

Kavindra Palaraja kdpalara at trolltech.com
Tue Mar 10 17:32:48 CET 2009


phil prentice wrote:
> Hi all
>   I need to create a variable length context menu.  I have used code like this 
> to do it:-
>
>   for(int i = 0; i < count; i++)
>   {
>     action = instrumentMenu.addAction(array[i]);
>    /* connect(action, SIGNAL(triggered()), this, SLOT(instMenu())); */
>   }
>   CurInstLabel->setContextMenuPolicy(Qt::CustomContextMenu);
>   connect(CurInstLabel, SIGNAL(customContextMenuRequested(const QPoint &)),
>                              this, SLOT(showInstContextMenu(const QPoint &)));
>
>   My showInstContextMenu() slot looks like this.
>
>   void QTdcwb::showInstContextMenu(const QPoint &)
>   {
>     QAction *retAction= instrumentMenu.exec(QCursor::pos());
>   }
>
>   I know which menu option has been selected by the retAction pointer returned 
> from the exec.  Each menu choice has some other data associated with that 
> choice.  I could use retAction to identify which item was selected and then 
> access the appropriate data.  Is there a better way?  I cant use the 
> triggered signal because it wont tell me which menu item was selected 
> (without setting a slot up per action).
>
> I just need a means of accessing the data associated with a menu choice 
> (action), without having to do a search through the action list.
>
> Thanks
> Phil
>
>
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>   
Hi Phil,

If you are trying to have different actions but using the same signal,
you might be able to use QSignalMapper. Normally when you have different
buttons but all emit the "clicked()" signal, we recommend this class.

You can find out more about it here:
http://doc.trolltech.com/4.5/qsignalmapper.html

There's a nice explanation on how it works here:
http://doc.trolltech.com/4.5/signalsandslots.html#advanced-signals-and-slots-usage

Hope this helps,
Kavindra.




More information about the Qt-interest-old mailing list