调试 Qt 信号和插槽的最佳实践是什么? [英] What are some best practices for debugging Qt signals and slots?

查看:40
本文介绍了调试 Qt 信号和插槽的最佳实践是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调试信号和槽可能很困难,因为调试器在发出信号时不会跳转到信号的槽.调试 Qt 信号和槽的最佳实践有哪些?

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 and slots?

特别是

  1. 如何确保连接设置成功?
  2. 什么时候应该使用信号和槽,什么时候应该避免它们?
  3. 根据您的经验,最有效的调试技术是什么?

推荐答案

前段时间有一篇博文叫20 种调试 Qt 信号和插槽的方法
它解决了我认为的第 1 和第 3 个问题.

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.

对于#2,我认为使用或不使用信号/插槽确实没有一个硬性和快速的理由,因为它们是 GUI 框架的一个非常核心的概念.信号是将一个组件的知识与另一个分离的完美方式,允许您设计可重用的小部件,这些小部件只需声明状态更改或通知.通过发出主线程可以看到的信号,这也是从非 GUI 线程循环传达 GUI 更改的一种非常好的方法.

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:

  • 您处理"事件
  • 您会收到有关"信号发射的通知

我喜欢这句话的地方在于它描述了不同的需求案例.如果您需要处理小部件中的操作,那么您可能需要一个事件.如果您想在发生的事情时收到通知,那么您可能需要一个信号.

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.

这篇关于调试 Qt 信号和插槽的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆