调试Qt信号和插槽有哪些最佳实践? [英] What are some best practices for debugging Qt signals and slots?

查看:132
本文介绍了调试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. 什么是信号和插槽


推荐答案

有一段博客写了一段时间称为调试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天全站免登陆