Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Map Signals Based On Parameter Value

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.

like image 250
erelender Avatar asked Oct 14 '09 11:10

erelender


2 Answers

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.
like image 174
rpg Avatar answered Oct 17 '22 00:10

rpg


Qxt has a class exactly for this functionality. You can use QxtSlotMapper class which is in QxtCore module.

http://libqxt.bitbucket.org/doc/tip/qxtslotmapper.html

like image 37
Nejat Avatar answered Oct 17 '22 00:10

Nejat