[Qt-interest] How To: pass extra data to object slots from menu action signals?

Malyushytsky, Alex alex at wai.com
Tue Feb 2 02:16:11 CET 2010


If you want to be elegant you can use QSignalMapper to map pointer to your QAction using
QObject * QSignalMapper::mapping ( QObject * object ) const, then you do the same thing as you done with sender but instead sender() you are using qobject_cast

The only negative of using sender() - logic fails if somebody call slot directly.
This breaks the object-oriented principle of modularity.
In cost of QSignalMapper maintenance you can avoid calling sender().


If QSignalMapper is too heavy (it does maintain extra map) it can be simplified, without breaking modularity.

If all you need is to call another slot with adding the pointer to QObject which sent the signal, You don't really need to maintain any map as QSignalMapper does.

In such class all slot 'map()' does its emits mapped( sender () ) signal.
Make map() private and there is no way to call such slot outside of the mapper class instance except by receiving properly connected signal.

This way might be preferable if you deal with a lot of objects when extra mapping increases time and memory usage significantly, but you still want to be elegant as much as possible.

As a side note I am not sure how sender() works for Qt::QueuedConnection.

Regards,
    Alex








-----Original Message-----
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of kent williams
Sent: Monday, February 01, 2010 2:07 PM
To: qt-interest at trolltech.com
Subject: Re: [Qt-interest] How To: pass extra data to object slots from menu action signals?

I found a way around this problem, but somehow it feels sneaky:

1. Derive a new class from QAction, and add the data I need to this class.
2. Add my derived-from-QAction class to the QMenu after setting the member data.
3. In the action connected to my action, cast sender() to my derived
type, and retrieve the needed data from it.

As elegant as the signal/slot paradigm is, I'd think there would be a
more 'Qt' way of doing this.

On Mon, Feb 1, 2010 at 3:35 PM, kent williams <nkwmailinglists at gmail.com> wrote:
> I am working on a program that dynamically adds and removes actions
> from menus, based on adding and deleting objects in the user
> interface.
>
> This causes a problem in QT -- actions attached to a QMenu send a
> triggered() signal, but that signal can only be connected to a slot in
> my class with a similar signature, e.g. triggered() or triggered(bool
> b).
>
> I figured out that I could use a QSignalMapper to map triggered() from
> a menu action to a slot that takes a single string argument.  But what
> I'd like to do is pass in two strings. In other words I have a
> multilevel menu like this
>
> File
>    Save
>       Type1
>           Object1
>           Object2
>       Type2
>           Object1-1
>           Object1-2
>
> And I want to trigger this slot
>
> void SaveSomething(Type, ObjectName);
>
> What I'm asking is this: is there a way to have a Signal send more
> information to an object?
>
> The only thing I could figure out is to construct strings containing
> both arguments, and then use QSignalMapper to map that Action to that
> string.
>
> Is there a better way?
>

_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest


---------------------------------------------------------------------------------------------------
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