PDA

View Full Version : Problem to recover an object via a QAction



Potch
3rd July 2008, 16:33
Hello,

I have several QPushButtons on which I added the same contextual menu this way :


setContextMenuPolicy(Qt::ActionsContextMenu);
QAction* MyAction = new QAction("DoThis");
connect(MyAction, SIGNAL(triggered()), this, SLOT(MySlot()));
addAction(MyAction);


For every button, there's a contextual menu with the same action. This action is connected to one slot. This slot has to behave differently regarding to the sender.

So far I can easily recover the occurrence of the button the user clicked on using :


QPushButton* PB = qobject_cast<QPushButton*>( sender() );
PB->etc...


My problem is to recover the occurence of the button on which the user used the contextual menu. For QAction inherits QObject, I tried


QAction* TheAction= qobject_cast<QAction*>( sender() );

But then how to make the link between the QAction and the QPushButton ?
I associated the same action to all the buttons. Is it a problem ?

Please feel free to ask me if I didn't made myself clear.
Thanks in advance.

caduel
3rd July 2008, 17:22
How about QAction::setData?
You can set some specific data (an enum perhaps), when you create and add the action, later you can retrieve it.


(PS: Personally I do not really like using sender() etc. and prefer using QSignalMapper if necessary.)

HTH

Potch
4th July 2008, 12:19
Thanks for your advice caduel, I'm seeing how I can implement it this way...