每当将 QWidget 添加到 QApplication 的小部件树时如何接收事件? [英] How to receive event whenever a QWidget is added to QApplication's widget tree?

查看:23
本文介绍了每当将 QWidget 添加到 QApplication 的小部件树时如何接收事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在应用范围内检查一些东西.具体来说,我想检查添加到应用程序中的每个小部件.

I want to inspect something application wide. Specifically I'd like to inspect every widget that is added to the application.

类似的事情可以在 JavaScript/HTML 中完成,您可以在其中添加 DOM 突变侦听器,在 DOM 更改时触发.

Similar thing can be done in JavaScript/HTML, where you can add DOM mutation listener which fires on DOM changes.

我可以在 QApplication 上监听添加的小部件吗?具体来说,捕获添加到 QApplication 对象的小部件作为子级或添加到任何顶级小部件或他们的子级、子子级等的小部件...

Can I listen on added widgets upon QApplication? Specifically, capture widgets added to QApplication object as children OR widgets added to any of the top level widgets or their children, subchildren, etc...

如果不可能,最好的解决方法是什么?

If not possible, what is best workaround available?

推荐答案

最稳定的解决方案可能是每隔一段时间遍历对象层次结构,以 QApplication 作为根开始,并且用你的谓词检查每个对象.这将是低效的,这就是为什么我要求提供有关您实际想要查询的对象的更多信息的原因.另一方面,对于一个测试框架,你可能不太在意效率.

The most stable solution is likely to be to walk the object hierarchy every so often, starting with the QApplication as the root, and check each object with your predicate. This is going to be inefficient, which is why I asked for more information about what objects you actually want to query. On the other hand, for a test framework, you may not care so much about efficiency.

不过,也许可以做其他事情.您可以在任何 QObject 上安装事件过滤对象,这些对象定义了当它们从 Qt 的事件系统接收到事件时的响应方式.您可以在根 QApplication 对象上安装一个事件过滤器(并在创建的任何子对象上递归),以检查事件是否是 QChildEvent 以及是否 added() 为它返回 true.如果这是真的,这意味着添加了一个新孩子,那么您也可以将此事件过滤器安装到该孩子上.所有其他事件将原封不动地传递.您可以在此处阅读有关安装事件过滤器的更多信息.本质上,过滤器是一个 QObject 子类,它定义了 eventFilter() 函数,对于应该过滤(停止)的事件返回 true,否则返回 false.当返回 true 时,在新创建的对象上运行您的谓词并在其上安装相同的事件过滤器.

It might be possible to do something else, however. You can install event-filtering objects on any QObject, which define how they respond when they receive an event from Qt's event system. You could install an event filter on the root QApplication object (and recursively on any children created) that would check if the event is a QChildEvent and if added() returns true for it. If that's true, meaning a new child was added, you could then install this event filter onto that child as well. All other events would be passed on untouched. You can read more about installing event filters here. Essentially, a filter is a QObject subclass that defines the eventFilter() function, which returns true for events that should be filtered (stopped) and false otherwise. When this returns true, run your predicate on the newly-created object and install the same event filter on it.

需要注意的一个问题是 eventFilter() 函数只接收 QObjects.您可以通过调用 isWidgetType() 来了解对象是否为 QWidget,但您不知道更多.因此,只要您的谓词只能使用为这些通用基类定义的方法和数据,那应该没问题.

One issue to be aware of with this is that the eventFilter() function only receives QObjects. You can learn if the object is a QWidget by calling isWidgetType(), but you don't know more than that. So as long as your predicate can make do with only methods and data defined for these general base classes, that should be OK.

这篇关于每当将 QWidget 添加到 QApplication 的小部件树时如何接收事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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