[Qt-interest] Which Action?

Malyushytsky, Alex alex at wai.com
Thu Oct 6 03:51:39 CEST 2011


Using sender() might be convenient, but may be unsafe if you don’t add any measures to prevent
your slot which has call to sender() from being called as a regular function ( not as a reaction on a signal)

This why if you deal with Menu actions only, it is better to use QMenu triggered(QAction*) or
In general it is better to use QSignalMapper which supposed to hide sender() call.
(in fact my impression that QSignalMapper does not do it good enough, cause map() which internally calls sender() is public and can be called directly)

Still QSignalMapper would be a general choice when you don't need to handle a LOT of different senders otherwise rebuilding internal map is inefficient.
In rare case you do or your signals are not parameterless, you may write light-weight equivalent of QSignalMapper, the only purpose of which would be to hide a call of the sender in the private slot.

I would suggest your mapper class only one type of the sender pointer ( QObject * object )
( or at least no 'id' ).

In this case all you need to do is make sure that YourSignalMapper receives all the signals,
then call sender() and re-emit appropriate signal with sender pointer in a private slot.
Making it private will make impossible to call such slot as a function.

QSignalMapper attempts to add 'ID" concept and that is why it keeps hash, which is totally unnecessary if all you need is a pointer to the sender object.
If it was my choice I would completely remove such capability from Qt source code
or at least added QLightSignalMapper which would function as was described above and derived QSignalMapper from it for people who do need ID.
But this might be not easy if you need to keep binary compatibility, so I doubt this will happen.


Regards,
  Alex



From: qt-interest-bounces+alex=wai.com at qt.nokia.com [mailto:qt-interest-bounces+alex=wai.com at qt.nokia.com] On Behalf Of Szalata, Zenon M.
Sent: Monday, September 26, 2011 9:42 PM
To: MARTIN Pierre; qt-interest at qt.nokia.com
Subject: Re: [Qt-interest] Which Action?

Thanks again,
I continued looking for an example that would show how to do this and found one in the book on QT:
“C++ GUI Programming with Qt 4, second Edition”, by Jasmine Blanchette and Mark Summerfield.
Here is a snippet of how I implemented this and it works:

void MainWindow::_triggered(){
//------------------------------------------------------------------------------
  QAction* action=qobject_cast<QAction*>(sender());
  if(!action) return;
…
}

Where MainWIndow::_triggered() is a slot function connected with the triggered(bool) signal for all actions of interest.

Zen

From: qt-interest-bounces+zms=slac.stanford.edu at qt.nokia.com [mailto:qt-interest-bounces+zms=slac.stanford.edu at qt.nokia.com] On Behalf Of MARTIN Pierre
Sent: Monday, September 26, 2011 3:00 PM
To: qt-interest at qt.nokia.com
Subject: Re: [Qt-interest] Which Action?

In the actionTriggered() function I need to know which action triggered the call.
Is there a way to get that information in the actionTriggered() function?
Also, i suggest you look at the QMenu triggered(QAction*) signal, which sends a pointer to the QAction which has been triggered here:
http://doc.qt.nokia.com/latest/qmenu.html#signals
And i believe instead of connecting each individual action to a slot, you just connect the parent menu to a slot which will be called whatever action is triggered.

Pierre.



---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.

“This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you.”

“Please consider our environment before printing this email.”


More information about the Qt-interest-old mailing list