What are some best practices for debugging Qt signals and slots?

后端 未结 4 1862
青春惊慌失措
青春惊慌失措 2021-02-04 19:27

Debugging signals and slots can be hard, because the debugger does not jump to the slots of a signal when it is emitted. What are some best practices for debugging Qt signals an

4条回答
  •  情书的邮戳
    2021-02-04 19:57

    There was a blog post written a while back called 20 ways to debug Qt signals and slots
    It addresses I think #1 and #3 of your questions.

    For #2, I don't think there is really a hard and fast reason to use or not use signals/slots, as they are a pretty core concept of the GUI framework. Signals are a perfect way to decouple the knowledge of one component to another, allowing you to design reusable widgets that simply declare state changes or notifications. It is also a very good way to communicate GUI changes from non-GUI thread loops, by emitting a signal that the main thread can see.

    There might be times where what you really want instead of a signal/slot is to use events, such as when a parent widget should become the event filter for a number of child widgets. The children still do not need to know about the parent, and the parent gets a more direct event as opposed to a signal connection.

    On the same topic of events, there are times where what you really want is a bubbling up of an event from child -> parent -> grandparent -> etc. Signals would make less sense here because they are not meant as a way to determine whether the proposed event should result in an action (they could be used this way obviously). Events allow you to check the current state, decide whether this widget should do anything, or otherwise bubble them up the chain for someone else to inspect.

    There is a really fantastic answer about The Difference Between Signals/Slots and Events. Here is a good snippet:

    • You "handle" events
    • You "get notified of" signal emissions

    What I like about that quote is that it describes the different need case. If you need to handle an action in a widget, then you probable want an event. If you want to be notified of things happening, then you probably want a signal.

提交回复
热议问题