views:

305

answers:

2

Hi folks,

I know that i can use QSignalMapper to call a slot with different parameters based on connection. What i want to achieve is a little different.

We are using plugins in our application and different plugins are responsible for different types of objects. We are connecting multiple slots, each implemented in a different plugin, to one signal emitted by the main application. One of the parameters of the signal is a QString indicating the type of object associated with the signal. Currently, we are checking this parameter in the slots and proceed if the type is handled by the plugin. This has a downside, every plugin does this checking and i want to avoid this if possible.

I want to connect all slots to the same signal, and when the signal is emitted, only the appropriate slot is called depending on the value of the QString argument, kind of like a QSignalMapper but in a different way.

Is there any built-in mechanism to do this? If not, any ideas on how i can achieve this?

Thank you in advance.

+1  A: 

I don't think there's a component for that, but you could create your own signal mapper like this:

  1. create a MySignalMapper component
  2. code an addSourceSignal method to set the signal of the main app
  3. code an addDestinationSlot method that takes a QString/slot pair and maps the string to the slot.
  4. in your component connect the source signal to a custom slot that dispatches based on the qstring value. You can invoke a slot with QMetaObject::invokeMethod.
rpg
A: 

You could restrict the allowed values of the "type" string to be valid C++ function names only. Force the plugins to name their slots according to the types they handle, then for each plugin simply attempt to connect the appropriately named slots.

In other words, you could do what QMetaObject::connectSlotsByName, except implement your own naming convention.

Intransigent Parsnip