Function Requires Output from Multiple Signals?
-
Hi all,
I have been self-teaching myself Qt programming and I'm really struggling to understand how to design a GUI application around signals and slots. My application consists of a dock widget, and each widget within the dock displays some information to the user. These widgets can query the backend for data, and the data is emitted from the backend as a signal to a slot within the widget.
Easy case, require data from 1 signal: Whenever my widget receives a signal, it displays information to the screen. As part of the function prototype for the signal I have a result id as well as a "timestamp" so I can tell if 1) the requested data was for the correct widget, and 2) that the data is not stale and should be displayed. This all seems to work well. Whenever my slot is called, the data I need to display is passed in as a parameter.
Hard case, require data from multiple signals: I have a widget which requires input from multiple signals and I'm not sure how to handle this case. I'm thinking of having two slots in the widget one for each signal. When either signal is called, I save the data, and then check to see if the other signal has already been called. If it has, great, proceed to the display routine. If it has not, I think I just save the data within the widget class, return, and hope that the second signal will be called soon.
How do I handle error conditions? What if one of the two signals never shows up? Thanks in advance, please let me know if you need me to help clarify what i'm after.
-
Hi @soupcan :)
@soupcan said:
I'm thinking of having two slots in the widget one for each signal. When either signal is called, I save the data, and then check to see if the other signal has already been called.
That's what I would do :)
@soupcan said:
What if one of the two signals never shows up?
What do you want to happen?
You can do just about anything here, depending on what the two signals represent. For example, if you want nothing to happen when one signal never show's up, then you're done - do nothing :) Or, for example, if you want some sort of timeout, use one of Qt's timer mechanisms (like QTimer or QObject::startTimer) to signal a third slot, and have that slot do whatever makes sense for your user case.
I hope that helps.
Cheers.
-
Depending on how complex things get, it might make sense to have a "data aggregation layer" between logic and UI. Basically, a UI element would only be connected to a single signal of the aggregation layer, and the aggregation layer would keep track of data changes, and would know when to emit that one signal.
It doesn't reduce complexity, but it cleanly distributes it between two parts of your program.