简体   繁体   English

轻量级QObject-兼容的基类

[英]Lightweight QObject - compatible base class

QObject's versatilty causes it to be fairly large. QObject的多功能性使其相当大。

I want to use it only for static properties (essentially just QMetaObject), without incurring the massive 120 byte penalty for inheriting from QObject. 我只想将其用于静态属性(基本上只是QMetaObject),而不会因从QObject继承而产生120字节的巨额罚款。

Is there a lightweight equivalent? 有轻量级的等效物吗?


Edit 编辑

Here are the profiling results - it's painful to have ~30MB wasted for a mid-size task (some can be ~20x larger). 这是分析结果-中型任务浪费了约30MB的内存(有些可能会增大20倍之多),这很痛苦。 Note the b-written ratio - the QObject constructor sets a lot of state that is almost never inspected. 注意b书写的比率-QObject构造函数设置了很多状态,而这些状态几乎从未被检查过。

==10302== ======== ORDERED BY decreasing "max-bytes-live": top 10 allocators ========
==10302==
==10302== -------------------- 1 of 10 --------------------
==10302== max-live:    31,264,224 in 229,884 blocks
==10302== tot-alloc:   31,264,224 in 229,884 blocks (avg size 136.00)
==10302== deaths:      none (none of these blocks were freed)
==10302== acc-ratios:  0.01 rd, 1.16 wr  (459,768 b-read, 36,321,672 b-written)
==10302==    at 0x4C275C0: operator new(unsigned long) (in /usr/lib64/valgrind/vgpreload_exp-dhat-amd64-linux.so)
==10302==    by 0x53E1551: QObject::QObject(QObject*) (in /usr/lib64/qt4/libQtCore.so.4.8.5)
==10302==    by 0x50465CA: Util::Util() (util.h:84)

You could take a look at Q_GADGET (Relevant for Qt versions < 5.5) 您可以看一下Q_GADGET (与<5.5版的Qt相关)

Use Q_GADGET instead of Q_OBJECT to enable the meta object system's support for enums in a class that is not a QObject subclass. 使用Q_GADGET而不是Q_OBJECT来启用元对象系统对不是QObject子类的类中的枚举的支持。 Q_GADGET makes a class member, staticMetaObject, available. Q_GADGET使类成员staticMetaObject可用。 staticMetaObject is of type QMetaObject and provides access to the enums declared with Q_ENUMS. staticMetaObject类型为QMetaObject,并提供对使用Q_ENUMS声明的枚举的访问。 Q_GADGET is provided only for C++. Q_GADGET仅提供用于C ++。

You can also set/get Q_FLAGS and Q_CLASSINFO this way, but you can't use Q_PROPERTY, you simply need to derive from QObject for that. 您也可以通过这种方式设置/获取Q_FLAGS和Q_CLASSINFO,但是您不能使用Q_PROPERTY,您只需要为此从QObject派生。

Edit: 编辑:

Qt 5.5 added the following extra functionality: Qt 5.5添加了以下额外功能:

Qt Core: You can now have Q_PROPERTY and Q_INVOKABLE within a Q_GADGET, and there is a way to query the QMetaObject of such gadget using the QMetaTYpe system. Qt核心:现在,您可以在Q_GADGET中包含Q_PROPERTY和Q_INVOKABLE,并且有一种方法可以使用QMetaTYpe系统查询此类小工具的QMetaObject。

A QObject is not a value class. QObject不是值类。 It is meant to be used for its behaviors. 它旨在用于其行为。 It can often act as a facade to non-object instances. 它通常可以充当非对象实例的外观。 For example, a single object instance can act as an event filter for multiple other objects. 例如,单个对象实例可以充当多个其他对象的事件过滤器。 Thus, even in fairly complex systems, you won't have very many QObject instances. 因此,即使在相当复杂的系统中,您也不会有很多QObject实例。 Its "weight" then isn't that much of a concern. 那么它的“重量”就不是那么重要了。

An object's size depends on how it's used. 对象的大小取决于其使用方式。 An object without connections or dynamic properties takes less space than one with connections or dynamic properties. 没有连接或动态属性的对象比具有连接或动态属性的对象占用的空间更少。 The space requirement must be determined by instrumenting the memory allocator or inspecting the code. 必须通过检测内存分配器或检查代码来确定空间需求。 A QObject is a handle class and by itself has a size of two pointers (a d-pointer and a vtbl-pointer). QObject是一个句柄类,其本身的大小为两个指针(d指针和vtbl指针)。 The PIMPL takes additional memory, as does the pimpl extension that doesn't get allocated by default. PIMPL会占用额外的内存,默认情况下未分配的pimpl扩展也是如此。

For a sense of perspective, the size of an otherwise empty class instance with virtual methods is 8 bytes on a 64 bit system. 从一个角度来看,在64位系统上,带有虚拟方法的本来是空的类实例的大小为8个字节。 A QObject that's merely an order of magnitude larger than a pointer is, I'd say, a steal . 我想说,仅比指针大一个数量级的QObject就是窃取对象 Calling it heavy is IMHO preposterous :) On my machine, an empty std::map<std::string, QVariant> is 1/3 the size of a QObject . 称它为IMHO很荒谬:)在我的机器上,空的std::map<std::string, QVariant>大小是QObject 1/3。

If all you want is a static metamethod mechanism, you're probably trying too hard to leverage moc. 如果您想要的只是一个静态的元方法机制,那么您可能在尝试过度利用moc。 You could use a code generator, like, say, the excellent gsl and make your own. 您可以使用代码生成器(例如出色的gsl)并自己制作。 If your memory requirements are so critical, you'll need a custom solution anyway. 如果您的内存要求非常重要,那么无论如何您都需要一个自定义解决方案。 You better had performance and profiling information to back up your desire not to re-use QObject , though. 不过,您最好具有性能和配置文件信息来支持您不重复使用QObject愿望。 If you wish to build gsl using qmake, here's a qmake project for it . 如果您希望使用qmake来构建gsl,那么这里有一个qmake项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM