[PyQt] Signal filtering based on arguments

Giuseppe Corbelli giuseppe.corbelli at copanitalia.com
Thu Oct 9 08:33:45 BST 2014


On 08/10/2014 08:20, Florian Bruhin wrote:
> Hi,
>
> I have an issue which I don't really know to solve properly, maybe
> someone has an idea here.
>
> My application has a configuration structured into sections and
> options. The user can change the config from inside the application,
> and everything should update/reinitialize correctly on a change, so no
> restart of the application is needed.
>
> I have a global Config object which stores the configuration and has a
> "changed" signal, with the section/option as a parameter.
>
> Now I have a lot of "on_config_changed" slots in many different
> objects, and most of the time these are only interested in a few
> options - only if these are changed they have to take action (for
> example call setStyleSheet again because a configured color/font
> changed).
>
> My original solution was simply having these slots, connecting them in
> a central place (where I create the config object) and checking inside
> each slot if it is interested in the event.
>
> As this was rather unpractical, I tried switching to a solution where
> every object calls  `config.on_change(section, option, callback)`. The
> config module then saves a (section, option, callback) tuple in a list
> of callback handlers, and calls the appropriate callbacks on a change.

Maybe staying with the original signal/slot implementation but use a factory 
to create the objects thus avoiding explicit connect()s?

Suppose you have a single class tree you may keep a single on_config_changed() 
slot in the base class, and redefine for each subclass a map (section, option) 
-> slot_to_be_called

Something like:

class Base:
   MYMAP = {}
   def on_config_changed(self, section, option):
     slot_name = self.MYMAP[(section, option)]
     getattr(self, slot_name)(section, option)


class Derived(Base):
   MYMAP = {
     ('ASection', 'AnOption): 'AMethod'
   }

   def AMethod(self, section, option):
     pass

-- 
             Giuseppe Corbelli
WASP Software Engineer, Copan Italia S.p.A
Phone: +390303666318  Fax: +390302659932
E-mail: giuseppe.corbelli at copanitalia.com


More information about the PyQt mailing list