gpt4 book ai didi

c++ - Qt 事件和信号/插槽

转载 作者:IT老高 更新时间:2023-10-28 12:01:16 32 4
gpt4 key购买 nike

Qt世界中,事件和信号/槽的区别是什么?

一个会取代另一个吗?事件是信号/槽的抽象吗?

最佳答案

Qt documentation可能解释得最好:

In Qt, events are objects, derived from the abstract QEvent class, that represent things that have happened either within an application or as a result of outside activity that the application needs to know about. Events can be received and handled by any instance of a QObject subclass, but they are especially relevant to widgets. This document describes how events are delivered and handled in a typical application.



所以事件和信号/槽是完成相同事情的两个并行机制。通常,事件将由外部实体(例如,键盘或鼠标滚轮)生成,并通过 QApplication 中的事件循环传递。 .通常,除非您设置代码,否则您不会生成事件。您可以通过 QObject::installEventFilter() 过滤它们或通过覆盖适当的函数来处理子类对象中的事件。

信号和插槽更容易生成和接收,您可以连接任意两个 QObject子类。它们是通过元类处理的(更多信息请查看您的 moc_classname.cpp 文件),但是您将生成的大多数类间通信可能会使用信号和槽。信号可以立即传递或通过队列延迟传递(如果您正在使用线程)。

可以产生信号。

关于c++ - Qt 事件和信号/插槽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3794649/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com