获取在应用程序中创建的所有 QObject 的列表 [英] Get a list of all QObjects created in a Application

查看:39
本文介绍了获取在应用程序中创建的所有 QObject 的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要获取在应用程序中创建的所有 QWidget 的列表,我们可以简单地调用 QApplication::allWidgets().

我已经阅读了文档,但我没有找到类似的内容来获取所有 QObject 的列表.如果应用程序创建了不是 QWidgets 的独立 QObjects,我就没有这样的功能可以使用.

有没有办法获得这样的列表?

解决方案

首先了解 QObjectQWidget 类之间的区别很重要.

类 QObject

QObject 实例有一个指向其父对象的指针,该指针可能是也可能不是空指针.创建一个没有父对象的 QObject 实例是非常合理的,只要您了解该实例的生命周期是您的责任,即如果您不删除它,您将泄漏内存.

类 QWidget

虽然看起来你可以在没有父级的情况下创建 QWidget,但你不能.当你创建一个没有父级"的 QWidget 实例时(即通过提供一个空指针或让它默认为一个空指针),它的父级有效地被设置为你的应用程序的 QApplication1 实例.><块引用>

有没有办法获得这样的列表?

没有.QApplication::allWidgets() 起作用的原因是所有 QWidget 实例都有一个指向它们的指针,该指针存储在您的单个 QApplication 实例中.没有 QObject 指针的中央存储,因此没有内置的方法来检索它们.

解决此问题的一种方法是您自己确保所有 QObjects 实例(或继承 QObject 的类的实例)都有一个有效的父级.然后你可以使用 QObject 函数 children(), findChild()findChildren() 以获取指向这些对象的指针.

这些是强大的工具,就像任何强大的工具一样,小心使用!在生产代码中使用这些对象之前,请确保您了解 C++ 对象生命周期.


<小时>

注意事项:

  1. QApplication 继承自 QCoreApplication,后者继承自 QObject.对于 Qt 框架如何使用 QApplication,存在一些限制.

To get a list of all the QWidgets created in an application we can simply call QApplication::allWidgets().

I've read the documentation, and I haven't found anything like this to get a list of all QObjects. If an application creates stand-alone QObjects that are not QWidgets I have no such function to use.

Is there a method to obtain such a list?

解决方案

To begin with it's important to understand the difference between the QObject and QWidget classes.

class QObject

QObject instances have a pointer to their parent which may or may not be a null pointer. It's quite legitimate to create a QObject instance without a parent so long as you understand that it is your responsibility for that instance's lifetime i.e. if you don't delete it you will leak memory.

class QWidget

Although it appears you can create a QWidget without a parent, you can't. When you create an instance of a QWidget "without a parent" (i.e. by supplying a null pointer or letting it default to a null pointer) it's parent effectively gets set to your application's QApplication1 instance.

Is there a method to obtain such a list?

No. The reason QApplication::allWidgets() works is due to the fact that all QWidget instances have a pointer to them which is stored in your single QApplication instance. There is no central store of QObject pointers and therefore no in-built way of retrieving them.

One way around this is to ensure yourself that all QObjects instances (or instances of classes which inherit QObject) have a valid parent. Then you can use the QObject functions children(), findChild() and findChildren() to get pointers to these objects.

These are powerful tools, and like anything powerful, use them with care! Make sure you understand C++ object life spans before you use these in production code.



Notes:

  1. QApplication inherits from QCoreApplication which inherits from QObject. There are some restrictions placed on how QApplication is used by the Qt framework.

这篇关于获取在应用程序中创建的所有 QObject 的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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