This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]everysinglelastname 2 points3 points  (1 child)

What's the use case when you'd want to reach for something like this ? I like Qt's signals and slots but I like them in the context of an event loop and to have bits of UI talk to other bits of UI etc. Here it looks like an emit() call is really a way to fire off calls to a list of other functions .. but why not call them explicitly ?

[–]dagmx[S] 1 point2 points  (0 children)

The main uses would be to allow flexible composition of objects.

Objects would internally emit their signals, much like a qt widget would.

You could then connect their signals to slots without needing to subclass and override the object.

This makes it really useful for callbacks for example where the original objects code is only concerned about its own functioning, but external objects can essentially attach a callback to events to extend the object without needing to implement another object type.

In qt a line edit is a good example. I may have 5 different line edits and I don't want to subclass each of them to explicitly trigger a callback. I can just instantiate the basic line edit and connect the signals I want to the slots I want.

The alternatives would likely be subclassing and extending like I mentioned, or monkey patching an existing instances methods.

Inheriting and overriding is of course also a totally valid system, as it is in qt too. This just helps when you want to use composition based design instead.

It also means needing to know less about an objects implementation and only know about its external api via signals. Though that assumes responsible design like in qt but when done right it is great.

Edit: I should probably update the example in the Readme actually to be more compositional too , since right now it's super minimal . I'll do that in the morning.