Q_DECLARE_METATYPE(Type)
This macro 宏 makes the type Type known to QMetaType as long as it provides a public default constructor, a public copy constructor and a public destructor. It is needed to use the type Type as a custom type in QVariant.
这个宏使得参数Type被QMetaType 元数据使用。相当于注册了一个类型,可以被其他数据结构使用。一般在QVariant中使用。
This macro requires that Type is a fully defined type at the point where it is used. For pointer types, it also requires that the pointed to type is fully defined. Use in conjunction with Q_DECLARE_OPAQUE_POINTER() to register pointers to forward declared types.
Ideally, this macro should be placed below the declaration of the class or struct. If that is not possible, it can be put in a private header file which has to be included every time that type is used in a QVariant.
Adding a Q_DECLARE_METATYPE() makes the type known to all template based functions, including QVariant. [
QVariant 属于 Qt 的Core模块,属于Qt的底层核心之一,ActiveQt、QtScript、QtDeclarative等都严重依赖于QVariant。 QVariant 可以保存很多Qt的数据类型,
]
Note that if you intend to use the type in queued signal and slot connections or in QObject’s property system, you also have to call qRegisterMetaType() since the names are resolved at runtime.
This example shows a typical use case of Q_DECLARE_METATYPE():
struct MyStruct
{
int i;
…
};
Q_DECLARE_METATYPE(MyStruct)
If MyStruct is in a namespace, the Q_DECLARE_METATYPE() macro has to be outside the namespace:
namespace MyNamespace
{
…
}
Q_DECLARE_METATYPE(MyNamespace::MyStruct)
Since MyStruct is now known to QMetaType, it can be used in QVariant:
MyStruct s;
QVariant var;
var.setValue(s); // copy s into the variant
…
// retrieve the value
MyStruct s2 = var.value();
Some types are registered automatically and do not need this macro:
下面的数据类型是Qt中已经自动注册过的,不需要使用这个宏了。
Pointers to classes 类指针 derived 衍生出 from QObject
QList, QVector, QQueue, QStack, QSet or QLinkedList where T is a registered meta type 注册过的元类型
QHash