In particular, in response to the return values, I did not feel that the Qt signals and the slot create a good mechanism for moving back and forth. As others have noted, they work great in the observer / role structure. If you think about it, there are potential problems with return values ββ... for example, what happens when you have more than one slot connected to a signal and each of them returns different values? The same problems arise in the SigC ++ and Boost :: Signals libraries, where everyone has a mechanism (another?) To handle this, or at least figure out what you get.
If you really need to return a value, there are two relatively good ways I've found to manage it. First, use the Qt event mechanism to query the data, and then pass the requested data in response. Note that events do not have a built-in way to execute responses, so you will need to extend QEvent to be able to handle this case. We really do this in numbers of applications at my work. It works well enough for a threaded application when you need an asynchronous mechanism. The disadvantage of this method is that the requestor must know about the object with which he is requesting data (to send an event). The response object does not need to know more than the request, and how to send a response, which is usually encoded into the request. Note that this is almost the opposite of the observer pattern.
The second method is to pass a functor object to an object that should be able to request data. This works much better with a good abstract functor library that allows you to encapsulate objects and functions, such as the aforementioned SigC ++ or Boost :: Signals. It is also well suited for situations where an immediate response is required - you run a functor statement and it returns a value before continuing with the function. It also allows you to write an object similar to how you would do with a signal / slot mechanism, where an object that needs a return value from a function (signal) does not need to know anything about where the functor (signal connection) did or has come. It just starts the functor as needed.
source share