views:

44

answers:

2

Let's say we have the follogin UI:

+--------------------------+
|W1       +--------------+ |
|         |W2            | |
|         | +----------+ | |
|         | |W3        | | |
|         | +----------+ | |
|         |              | |
|         +--------------+ |
+--------------------------+

W3 is interested in a certain signal emited in W1 (or a level below, i.e. qApp).

The idea is to develop W3 independently. But somebody has to do the signal/slot connection.

What would be a good practice/recommended way of connecting the signal emited in W1 into slot in W3 if we want W3 not to know about any other widget, and we don't want W1 to know about W3 either?

Solution 1: Connect signal in W1 with signal in W2 (signal to signal technique) and therefore connect signal in W2 to slot in W3.

Solution 2: Generate a qApp signal and connect it in W2 with slot in W3.

Solution 3: Generate a qApp signal and have W3 itself connect to it using its own slot.

Thanks

+1  A: 

The approach we use here is that "the shell" makes the connections.

The shell is an object that knows about all the widgets involved. In this case, W1, W2, and W3. Usually it is the code that assembles the user interface.

If the shell doesn't know about W3 (because W3 is an implementation detail, for example) then W3's "owner" should make the connection and so on.

andref
Thanks for the response.The issue I find with your approach is that, if W3 contains another widget W4, which in turn contains W5, and so on, and the last widget in the chain needs to know about the signal emited from W1, it would mean to make a connection at every level. Is this acceptable in common practices?
Daniel
Are W4, W5, etc... part of the public interface of W3? If they are, and the connections are not required for the whole composite to work, then do it outside of W3. Else, encapsulation will be violated.
andref
+3  A: 

Usually the enclosing widget encapsulates the widgets inside it and provides a higher-level interface. E.g., if W3 contains several widgets that need to be enabled/disabled depending on some state, W3 would manage that state, provide API for it and enable/disable widgets accordingly. So usually there is no need to go directly from W1 to W3.

If the widgets don't know about each other (e.g. b/c W1 is a generic container widget that can embed anything), then the one who assembles the UI, knowing all involved widgets, should do the connection.

I don't know what you mean by "qApp signal", but that sounds too much like on central object being connected to everything, which of course isn't good design either.

Maybe you have a specific example you had in mind?

Frank
Thanks for your answer. I think it makes sense to just encapsulate what W3 needs in W2. W2 just needs to expose a slot, and whoever is connecting them would be in charge of that slot to be called. After that, it would be the job of W2 to use this information for W3. *** qApp is a macro that comes with Qt to have access to the QApplication object, and is accessible from anywhere in the code that has its roots in it. Signals emited in the QApplication instance can be connected by doing: connect(qApp, SIGNAL(someCustomSignal() ), this, SLOT(someSlot()));
Daniel
Yeah, then I understood your qApp idea correctly. (and my critical argument against it applies ;)). Personally I avoid using QApplication in the widget as far as possible. Assuming a certain QApplication subclass with custom signals/slots ties it even more together.Usually I don't subclass QApplication at all.I rather let some widgets know about each other than tying them together implicitely and well hidden via an allmighty QApplication object.
Frank