Search code examples
qtuser-interfacesignalsslot

signals and slots in multilayered UI widgets


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


Solution

  • 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?