如何枚举Qt中所有的QObject类?
有没有办法枚举应用程序或 DLL 中声明的所有 QObject 类?我正在尝试创建一个加载 DLL 并列出 DLL 内的所有 QObject 类的应用程序。
更新:实际上我正在尝试创建一个单元测试 GUI。它将加载 DLL,实例化内部的 QObject,并针对它们调用 QTest::qExec。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果程序代码没有被剥离,您可以通过阅读二进制文件进行一些内省:
objdump -demangle=C++ -t SomeQtLibrary.so |grep qt_static_metacall 它大致显示了 QObject 派生类。我认为他们都实现了这个符号。当然,由于您使用的是 Windows,因此您必须使用 Windows 工具,例如
nm
(如果我错了,请纠正我)。当然,在代码中处理符号也是可能的,但这是一个单独的主题。例如,我提到的命令会返回以下内容:
如果您对运行时自省感兴趣,则必须使用 QMetaObject 类。
对于注册为 QMetaType 的对象(仅限这些!),您可以执行一些操作额外的魔法让它们栩栩如生。不是很容易,也不是开箱即用的——但对于这样一种静态语言来说,它仍然是地狱般的。以下是 Qt 文档的片段(将
if(id == 0)
更改为if(id)
)。If the program code is not stripped you can get some instrospection by reading the binary:
objdump -demangle=C++ -t SomeQtLibrary.so |grep qt_static_metacall
it shows roughly the QObject derived classes. I think they all implement this symbol. Of couse since you are on Windows you would have to use Windows tools such asnm
(correct me if I am wrong). Naturally processing the symbols in-code is also possible, but this is a separate topic.The command I mentioned returns this for example:
If you are interested in run-time introspection than you would have to play with the QMetaObject class on each object.
For objects that were registered as QMetaType (and only those!) you can do some additional magic to bring them to life. Not very easy, and not out of the box - but it is still hell of a lot from such a static language. Here is a snippet from Qt docs (changed
if(id == 0)
toif(id)
).