- QMetaType 类
- 公共成员类型
- 公共成员函数
- 静态公共成员
- 相关非成员函数
- 宏定义
- 详细描述
- 成员类型文档
- 成员函数文档
- QMetaType::QMetaType(const int typeId = QMetaType::UnknownType)
- QMetaType::~QMetaType()
- [static] bool QMetaType::compare(const void *lhs, const void *rhs, int typeId, int *result)
- [static] void *QMetaType::construct(int type, void *where, const void *copy)
- void *QMetaType::construct(void *where, const void *copy = 0) const
- [static] bool QMetaType::convert(const void *from, int fromTypeId, void *to, int toTypeId)
- [static] void *QMetaType::create(int type, const void *copy = nullptr)
- void QMetaType::create(const void **copy = 0) const
- [static] bool QMetaType::debugStream( QDebug &dbg, const void *rhs, int typeId)
- [static] void QMetaType::destroy(int type, void *data)
- void QMetaType::destroy(void *data) const
- [static] void QMetaType::destruct(int type, void *where)
- void QMetaType::destruct(void *data) const
- [static] bool QMetaType::equals(const void *lhs, const void *rhs, int typeId, int *result)
- QMetaType::TypeFlags QMetaType::flags() const
- [static] template \
QMetaType QMetaType::fromType() - [static] template \
bool QMetaType::hasRegisteredComparators() - [static] bool QMetaType::hasRegisteredComparators(int typeId)
- [static] bool QMetaType::hasRegisteredConverterFunction(int fromTypeId, int toTypeId)
- [static] template
bool QMetaType::hasRegisteredConverterFunction() - [static] template \
bool QMetaType::hasRegisteredDebugStreamOperator() - [static] bool QMetaType::hasRegisteredDebugStreamOperator(int typeId)
- int QMetaType::id() const
- [static] bool QMetaType::isRegistered(int type)
- bool QMetaType::isRegistered() const
- bool QMetaType::isValid() const
- [static] bool QMetaType::load( QDataStream &stream, int type, void *data)
- const QMetaObject *QMetaType::metaObject() const
- [static] const QMetaObject *QMetaType::metaObjectForType(int type)
- ::QByteArray QMetaType::name() const
- [static] template \
bool QMetaType::registerComparators() - [static] template \
bool QMetaType::registerConverter() - [static] template \
bool QMetaType::registerConverter(MemberFunction function) - [static] template \
bool QMetaType::registerConverter(MemberFunctionOk function) - [static] template \
bool QMetaType::registerConverter(UnaryFunction function) - [static] template \
bool QMetaType::registerDebugStreamOperator() - [static] template \
bool QMetaType::registerEqualsComparator() - [static] bool QMetaType::save( QDataStream &stream, int type, const void *data)
- [static] int QMetaType::sizeOf(int type)
- int QMetaType::sizeOf() const
- [static] int QMetaType::type(const char *typeName)
- [static] int QMetaType::type(const ::QByteArray &typeName)
- [static] QMetaType::TypeFlags QMetaType::typeFlags(int type)
- [static] const char *QMetaType::typeName(int typeId)
- 相关非成员函数
- 宏定义文档
- 已废弃成员
QMetaType 类
QMetaType 类管理元对象系统中的注名类型。更多内容…。
| 属性 | 内容 |
|---|---|
| 头文件 | #include <QMetaType> |
| qmake | QT += core |
注意: 此类中所有函数都是线程安全的。
公共成员类型
| 类型 | 名称 |
|---|---|
| enum | Type { Void, Bool, Int, UInt, Double, …, UnknownType } |
| enum | TypeFlag { NeedsConstruction, NeedsDestruction, MovableType, IsEnumeration, PointerToQObject } |
| flags | TypeFlags |
公共成员函数
| 返回类型 | 函数 |
|---|---|
| QMetaType(const int typeId = QMetaType::UnknownType) | |
| ~QMetaType() | |
| void * | construct(void *where, const void *copy = 0) const |
| void * | create(const void *copy = 0) const |
| void | destroy(void *data) const |
| void | destruct(void *data) const |
| QMetaType::TypeFlags | flags() const |
| int | id() const |
| bool | isRegistered() const |
| bool | isValid() const |
| const QMetaObject * | metaObject() const |
| ::QByteArray | name() const |
| int | sizeOf() const |
静态公共成员
| 返回类型 | 函数 |
|---|---|
| bool | compare(const void *lhs, const void *rhs, int typeId, int *result) |
| void * | construct(int type, void *where, const void *copy) |
| bool | convert(const void *from, int fromTypeId, void *to, int toTypeId) |
| void * | create(int type, const void *copy = nullptr) |
| bool | debugStream(QDebug &dbg, const void *rhs, int typeId) |
| void | destroy(int type, void *data) |
| void | destruct(int type, void *where) |
| bool | equals(const void *lhs, const void *rhs, int typeId, int *result) |
| QMetaType | fromType() |
| bool | hasRegisteredComparators() |
| bool | hasRegisteredComparators(int typeId) |
| bool | hasRegisteredConverterFunction(int fromTypeId, int toTypeId) |
| bool | hasRegisteredConverterFunction() |
| bool | hasRegisteredDebugStreamOperator() |
| bool | hasRegisteredDebugStreamOperator(int typeId) |
| bool | isRegistered(int type) |
| bool | load(QDataStream &stream, int type, void *data) |
| const QMetaObject * | metaObjectForType(int type) |
| bool | registerComparators() |
| bool | registerConverter() |
| bool | registerConverter(MemberFunction function) |
| bool | registerConverter(MemberFunctionOk function) |
| bool | registerConverter(UnaryFunction function) |
| bool | registerDebugStreamOperator() |
| bool | registerEqualsComparator() |
| bool | save(QDataStream &stream, int type, const void *data) |
| int | sizeOf(int type) |
| int | type(const char *typeName) |
| int | type(const ::QByteArray &typeName) |
| QMetaType::TypeFlags | typeFlags(int type) |
| const char * | typeName(int typeId) |
相关非成员函数
| 返回类型 | 函数 |
|---|---|
| int | qMetaTypeId() |
| int | qRegisterMetaType(const char *typeName) |
| int | qRegisterMetaType() |
| void | qRegisterMetaTypeStreamOperators(const char *typeName) |
| bool | operator!=(const QMetaType &a, const QMetaType &b) |
| bool | operator==(const QMetaType &a, const QMetaType &b) |
宏定义
| 宏定义 |
|---|
| Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE(Container) |
| Q_DECLARE_METATYPE(Type) |
| Q_DECLARE_OPAQUE_POINTER(PointerType) |
| Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(Container) |
| Q_DECLARE_SMART_POINTER_METATYPE(SmartPointer) |
详细描述
此类是一个辅助类,被用作序列化 QVariant 以及队列连接信号槽中的类型。它将类型名称关联到对应类型,以支持运行时动态创建和销毁此类型。通过 Q_DECLARE_METATYPE() 声明新类型,让它可以被 QVariant 和其它模板函数(qMetaTypeId() 等)使用。调用 qRegisterMetaType() 来让其可以被非模板型函数使用,如信号槽的队列连接。
任何包含一个公共默认构造函数、一个公共拷贝构造函数、一个默认析构函数的类或结构体都可以被注册为元类型。
下述代码展示了如何分配和销毁一个 MyClass 的实例:
int id = QMetaType::type("MyClass");if (id != QMetaType::UnknownType) {void *myClassPtr = QMetaType::create(id);...QMetaType::destroy(id, myClassPtr);myClassPtr = 0;}
若我们想让流运算符 operator<<() 和 operator>>() 可被用于存储了自定义类型的 QVariant 对象,则这个自定义类型必须提供 operator<<() 和 operator>>() 运算符重载。
另请参阅:Q_DECLARE_METATYPE(),QVariant::setValue(),QVariant::value() 和 QVariant::fromValue().
成员类型文档
enum QMetaType::Type
下表是 QMetaType 内置支持的类型:
| 常量 | 数值 | 描述 | |
|---|---|---|---|
QMetaType::Void |
43 |
void |
|
QMetaType::Bool |
1 |
bool |
|
QMetaType::Int |
2 |
int |
|
QMetaType::UInt |
3 |
unsigned int |
|
QMetaType::Double |
6 |
double |
|
QMetaType::QChar |
7 |
QChar |
|
QMetaType::QString |
10 |
QString | |
QMetaType::QByteArray |
12 |
QByteArray | |
QMetaType::Nullptr |
51 |
std::nullptr_t |
|
QMetaType::VoidStar |
31 |
void * |
|
QMetaType::Long |
32 |
long |
|
QMetaType::LongLong |
4 |
long long |
|
QMetaType::Short |
33 |
short |
|
QMetaType::Char |
34 |
char |
|
QMetaType::ULong |
35 |
unsigned long |
|
QMetaType::ULongLong |
5 |
unsigned long long |
|
QMetaType::UShort |
36 |
unsigned short |
|
QMetaType::SChar |
40 |
signed char |
|
QMetaType::UChar |
37 |
unsigned char |
|
QMetaType::Float |
38 |
float |
|
QMetaType::QObjectStar |
39 |
QObject * | |
QMetaType::QVariant |
41 |
QVariant | |
QMetaType::QCursor |
74 |
QCursor | |
QMetaType::QDate |
14 |
QDate | |
QMetaType::QSize |
21 |
QSize | |
QMetaType::QTime |
15 |
QTime | |
QMetaType::QVariantList |
9 |
QVariantList | |
QMetaType::QPolygon |
71 |
QPolygon | |
QMetaType::QPolygonF |
86 |
QPolygonF | |
QMetaType::QColor |
67 |
QColor | |
QMetaType::QColorSpace |
87 |
QColorSpace(在 Qt 5.15 中被引入) | |
QMetaType::QSizeF |
22 |
QSizeF | |
QMetaType::QRectF |
20 |
QRectF | |
QMetaType::QLine |
23 |
QLine | |
QMetaType::QTextLength |
77 |
QTextLength | |
QMetaType::QStringList |
11 |
QStringList | |
QMetaType::QVariantMap |
8 |
QVariantMap | |
QMetaType::QVariantHash |
28 |
QVariantHash | |
QMetaType::QIcon |
69 |
QIcon | |
QMetaType::QPen |
76 |
QPen | |
QMetaType::QLineF |
24 |
QLineF | |
QMetaType::QTextFormat |
78 |
QTextFormat | |
QMetaType::QRect |
19 |
QRect | |
QMetaType::QPoint |
25 |
QPoint | |
QMetaType::QUrl |
17 |
QUrl | |
QMetaType::QRegExp |
27 |
QRegExp | |
QMetaType::QRegularExpression |
44 |
QRegularExpression | |
QMetaType::QDateTime |
16 |
QDateTime | |
QMetaType::QPointF |
26 |
QPointF | |
QMetaType::QPalette |
68 |
QPalette | |
QMetaType::QFont |
64 |
QFont | |
QMetaType::QBrush |
66 |
QBrush | |
QMetaType::QRegion |
72 |
QRegion | |
QMetaType::QBitArray |
13 |
QBitArray | |
QMetaType::QImage |
70 |
QImage | |
QMetaType::QKeySequence |
75 |
QKeySequence | |
QMetaType::QSizePolicy |
121 |
QSizePolicy | |
QMetaType::QPixmap |
65 |
QPixmap | |
QMetaType::QLocale |
18 |
QLocale | |
QMetaType::QBitmap |
73 |
QBitmap | |
QMetaType::QMatrix |
79 |
QMatrix | |
QMetaType::QTransform |
80 |
QTransform | |
QMetaType::QMatrix4x4 |
81 |
QMatrix4x4 | |
QMetaType::QVector2D |
82 |
QVector2D | |
QMetaType::QVector3D |
83 |
QVector3D | |
QMetaType::QVector4D |
84 |
QVector4D | |
QMetaType::QQuaternion |
85 |
QQuaternion | |
QMetaType::QEasingCurve |
29 |
QEasingCurve | |
QMetaType::QJsonValue |
45 |
QJsonValue | |
QMetaType::QJsonObject |
46 |
QJsonObject | |
QMetaType::QJsonArray |
47 |
QJsonArray | |
QMetaType::QJsonDocument |
48 |
QJsonDocument | |
QMetaType::QCborValue |
53 |
QCborValue | |
QMetaType::QCborArray |
54 |
QCborArray | |
QMetaType::QCborMap |
55 |
QCborMap | |
QMetaType::QCborSimpleType |
52 |
QCborSimpleType | |
QMetaType::QModelIndex |
42 |
QModelIndex | |
QMetaType::QPersistentModelIndex |
50 |
QPersistentModelIndex(在 Qt 5.5 中被引入) | |
QMetaType::QUuid |
30 |
QUuid | |
QMetaType::QByteArrayList |
49 |
QByteArrayList | |
QMetaType::User |
1024 |
用户类型的基础值(译者注:即起始值) |
` |
QMetaType::UnknownType |
0 |
这是无效的类型编号,QMetaType 会在类型未注册时返回此值。 |
可以使用 Q_DECLARE_METATYPE() 注册额外的类型。
enum QMetaType::TypeFlag
flags QMetaType::TypeFlags
此枚举类型描述了被 QMetaType 支持的类型的属性。
| 常量 | 数值 | 描述 |
|---|---|---|
QMetaType::NeedsConstruction |
0x1 |
此类型具有非平凡的构造函数。若某类型不具备此标志,则可通过 memset() 安全地清零。 |
QMetaType::NeedsDestruction |
0x2 |
此类型非平凡的析构函数。若某类型不具备此标志,则丢弃对象前不需要调用析构函数(译者注:即可以用 free() 释放对象) |
QMetaType::MovableType |
0x4 |
具有此标志的类型实例可以通过 memcpy() 安全地移动。 |
QMetaType::IsEnumeration |
0x10 |
此类型是枚举值。 |
QMetaType::PointerToQObject |
0x8 |
此类型是指向继承自 QObject 的类型的指针。 |
TypeFlags 类型是 QFlags\TypeFlag 值。
成员函数文档
QMetaType::QMetaType(const int typeId = QMetaType::UnknownType)
构造一个包含 typeId 对应的类型信息的 QMetaType 对象。
注意: 默认参数在 Qt 5.15 中被引入。
此函数在 Qt 5.0 中被引入。
QMetaType::~QMetaType()
析构此对象。
[static] bool QMetaType::compare(const void *lhs, const void *rhs, int typeId, int *result)
比较 lhs 和 rhs 对象,双方都需要是 typeid 中的类型。result 会被设为小于、等于或大于零,表示 lhs 小于、等于或大于 rhs。若比较成功则返回 true,否则返回 false。
此函数在 Qt 5.2 中被引入。
[static] void *QMetaType::construct(int type, void *where, const void *copy)
在给定的内存地址 where 上构造对应 type 类型的对象,该对象是 copy 的副本,并返回 where。若 copy 是空指针,则执行默认构造。
这是用于显示管理存储 type 类型对象的内存的底层函数。若不需要此类底层控制,则考虑使用 create() 函数(也就是指,使用 new 而非 placement new)。
您必须确保 where 指向的内存区域大小足够存储 type 对应的数据,并且 where 地址需要对齐,对应类型的大小可通过 sizeOf() 获取。
内存对齐的规则是对齐至类型的自然边界,也就是大于等于类型大小的2的n次方值,直至平台有效对齐宽度上限为止。对于特定用途来说,超过 2 * sizeof(void*) 的对齐宽度只是某些特定硬件指令所必需的(例如,x86 平台中对齐后的 SSE 读取和存储)。
此函数在 Qt 5.0 中被引入。
void *QMetaType::construct(void *where, const void *copy = 0) const
在给定的内存地址 where 上构造此 QMetaType 类型的对象,该对象是 copy 的副本,并返回 where。若 copy 是空指针,则执行默认构造。
这是用于显示管理存储 type 类型对象的内存的底层函数。若不需要此类底层控制,则考虑使用 create() 函数(也就是指,使用 new 而非 placement new)。
您必须确保 where 指向的内存区域大小足够存储 type 对应的数据,并且 where 地址需要对齐,对应类型的大小可通过 sizeOf() 获取。
内存对齐的规则是对齐至类型的自然边界,也就是大于等于类型大小的2的n次方值,直至平台有效对齐宽度上限为止。对于特定用途来说,超过 2 * sizeof(void*) 的对齐宽度只是某些特定硬件指令所必需的(例如,x86 平台中对齐后的 SSE 读取和存储)。
此函数在 Qt 5.0 中被引入。
[static] bool QMetaType::convert(const void *from, int fromTypeId, void *to, int toTypeId)
将 from 对象从 fromTypeId 转换至 toTypeId 并存储到预分配空间 to 中。若转换成功则返回 true,否则返回 false。
此函数在 Qt 5.2 中被引入。
[static] void *QMetaType::create(int type, const void *copy = nullptr)
假设 copy 的类型是 type,返回它的的拷贝。若 copy 是空指针,则返回默认构造的实例。
另请参阅:destroy(),isRegistered() 和 Type。
void QMetaType::create(const void **copy = 0) const
假设 copy 的类型是此 QMetaType ,返回它的的拷贝。若 copy 是空指针,则返回默认构造的实例。
此函数在 Qt 5.0 中被引入。
另请参阅:QMetaType::destroy()。
[static] bool QMetaType::debugStream(QDebug &dbg, const void *rhs, int typeId)
将 typeId 类型的 rhs 对象输出至调试流 debug,输出成功则返回 true,否则返回 false。
此函数在 Qt 5.2 中被引入。
[static] void QMetaType::destroy(int type, void *data)
假设 data 的类型是 type,销毁该对象。
另请参阅:create(),isRegistered() 和 Type。
void QMetaType::destroy(void *data) const
假设 data 的类型是此 QMetaType ,销毁该对象。
此函数在 Qt 5.0 中被引入。
另请参阅:QMetaType::create()。
[static] void QMetaType::destruct(int type, void *where)
假设 where 地址中存储的对象类型是 type,销毁该对象。
与 destroy() 不同,此函数只会执行该类型的析构函数,但不会执行 delete 运算符(译者注:即不会释放内存,与 placement new 相同机制)。
此函数在 Qt 5.0 中被引入。
另请参阅:construct()。
void QMetaType::destruct(void *data) const
假设 data 地址中存储的对象类型是此 QMetaType ,销毁该对象。
与 destroy() 不同,此函数只会执行该类型的析构函数,但不会执行 delete 运算符(译者注:即不会释放内存,与 placement new 相同机制)。
此函数在 Qt 5.0 中被引入。
另请参阅:QMetaType::construct()。
[static] bool QMetaType::equals(const void *lhs, const void *rhs, int typeId, int *result)
比较 lhs 和 rhs 对象,双方都需要是 typeid 中的类型。若 lhs 等于 rhs,则 result 会被设为零。若比较成功则返回 true,否则返回 false。
此函数在 Qt 5.5 中被引入。
QMetaType::TypeFlags QMetaType::flags() const
返回此 QMetaType 实例的类型标志。
此函数在 Qt 5.0 中被引入。
另请参阅:QMetaType::TypeFlags 和 QMetaType::typeFlags()。
[static] template \ QMetaType QMetaType::fromType()
返回模板类型 T 对应的 QMetaType 实例。
此函数在 Qt 5.15 中被引入。
[static] template \ bool QMetaType::hasRegisteredComparators()
若模板类型 T 已被注册至元对象系统则返回 true。
此函数在 Qt 5.2 中被引入。
[static] bool QMetaType::hasRegisteredComparators(int typeId)
若 typeId 的类型已被注册至元对象系统则返回 true。
此函数在 Qt 5.2 中被引入。
[static] bool QMetaType::hasRegisteredConverterFunction(int fromTypeId, int toTypeId)
若自 fromTypeId 到 toTypeId 的类型转换已被注册至元对象系统则返回 true。
此函数在 Qt 5.2 中被引入。
[static] template bool QMetaType::hasRegisteredConverterFunction()
若自模板类型 From 到 To 的类型转换已被注册至元对象系统则返回 true。
这是一个重载函数。
此函数在 Qt 5.2 中被引入。
[static] template \ bool QMetaType::hasRegisteredDebugStreamOperator()
若自模板类型 T 的 QDebug 流运算符已被注册至元对象系统则返回 true。
此函数在 Qt 5.2 中被引入。
[static] bool QMetaType::hasRegisteredDebugStreamOperator(int typeId)
若自 typeId 对应类型的 QDebug 流运算符已被注册至元对象系统则返回 true。
此函数在 Qt 5.2 中被引入。
int QMetaType::id() const
返回此 QMetatype 实例的类型编号。
此函数在 Qt 5.13 中被引入。
[static] bool QMetaType::isRegistered(int type)
若 typeId 对应已被注册至元对象系统则返回 true,否则返回 false。
另请参阅:type(),typeName() 和 Type。
bool QMetaType::isRegistered() const
若此 QMetaType 包含某类型的有效信息则返回 true,否则返回 false。
此函数在 Qt 5.0 中被引入。
bool QMetaType::isValid() const
若此 QMetaType 包含某类型的有效信息则返回 true,否则返回 false。
此函数在 Qt 5.0 中被引入。
[static] bool QMetaType::load(QDataStream &stream, int type, void *data)
从数据流 stream 中读取对应 type 类型的对象至 data 中,若读取成功则返回 true,否则返回 false。
此类型必须在这之前通过 qRegisterMetaType() 和 qRegisterMetaTypeStreamOperators() 完成注册。
通常来说,您不需要显示调用此函数,而是应使用 QVariant 的 operator>>(),该运算符依赖 load() 来传递自定义类型。
另请参阅:save() 和 qRegisterMetaTypeStreamOperators()。
const QMetaObject *QMetaType::metaObject() const
返回此类型对应的 QMetaObject。
若此类型是 QObject 子类的指针,即 flags() 包含 QMetaType::PointerToQObject,则此函数会返回对应类型的 QMetaObject。这可被用于结合 QMetaObject::construct(译者注:无此函数,请使用 QMetaObject::constructor 或 QMetaType::construct)来创建此类型的 QObject 实例。
若此类型是 Q_GADGET,即 flags() 包含 QMetaType::IsGadget(译者注:文档中未给出,但 QMetaType::TypeFlag 中的确包含此枚举值),则此函数会返回对应类型的 QMetaObject。这可以被用于获取 QMetaMethod 和 QMetaProperty,并将其用于此类型的对象指针上(例如通过 QVariant::data 获取指针 译者注:文档中无此函数,但此函数的确存在)。
若此类型是枚举,即 flags() 包含 QMetaType::IsEnumeration,且该枚举值是通过 Q_ENUM 注册的成员枚举类型,则此函数会返回其所属的 QObject 对象的元对象,否则返回 nullptr。
此函数在 Qt 5.5 中被引入。
另请参阅:QMetaType::metaObjectForType() 和 QMetaType::flags()。
[static] const QMetaObject *QMetaType::metaObjectForType(int type)
返回 type 类型对应的 QMetaType::metaObject。
此函数在 Qt 5.0 中被引入。
另请参阅:metaObject()。
::QByteArray QMetaType::name() const
返回此 QMetaType 对应的类型名称,若无有效类型则返回空指针。
此函数在 Qt 5.15 中被引入。
另请参阅:typeName()。
[static] template \ bool QMetaType::registerComparators()
将用户注册类型 T 的比较运算符注册至元对象系统。要求 T 具有 operator== 和 operator< 运算符。若注册成功则返回 true,否则返回 false。
此函数在 Qt 5.2 中被引入。
[static] template \ bool QMetaType::registerConverter()
将类型 From 到 To 的可能的隐式转换注册到元对象系统,若注册成功则返回 true,否则返回 false。
此函数在 Qt 5.2 中被引入。
[static] template \ bool QMetaType::registerConverter(MemberFunction function)
这是一个重载函数。
将形如 To From::function() const 的成员方法 function 作为从 From 到 To 的转换函数注册至元对象系统,若注册成功则返回 true,否则返回 false。
此函数在 Qt 5.2 中被引入。
译者注:
第二个模板参数是官方使用 doxygen 生成文档时的变通写法,实际代码中的函数签名是
template<typename From, typename To> static bool registerConverter(To(From::*function)() const)。使用时无需指定int模板参数,在函数参数中直接填入用于转换的成员函数指针即可。
[static] template \ bool QMetaType::registerConverter(MemberFunctionOk function)
这是一个重载函数。
将形如 To From::function(bool *ok) const 的成员方法 function 作为从 From 到 To 的转换函数注册至元对象系统,若注册成功则返回 true,否则返回 false。
此函数在 Qt 5.2 中被引入。
译者注:
第二个模板参数是官方使用 doxygen 生成文档时的变通写法,实际代码中的函数签名是
template<typename From, typename To> static bool registerConverter(To(From::*function)(bool*) const)。使用时无需指定char模板参数,在函数参数中直接填入用于转换的成员函数指针即可。
[static] template \ bool QMetaType::registerConverter(UnaryFunction function)
这是一个重载函数。
将把类型 From 转换为类型 To 的一元函数 function 注册至元对象系统,若注册成功则返回 true,否则返回 false。
此函数在 Qt 5.2 中被引入。
译者注:
原文描述地非常晦涩,实际指的是任何可被
To dst = function(src)方式调用的函数对象,包括全局函数、类静态函数、仿函数或 lamba 等,比上文另外两个registerConverter的约束更为宽松。
[static] template \ bool QMetaType::registerDebugStreamOperator()
将已注册类型 T 的 QDebug 流运算符注册至元对象系统,要求类型 T 具备流运算符 operator<<(QDebug dbg, T)。若注册成功则返回 true,否则返回 false。
[static] template \ bool QMetaType::registerEqualsComparator()
将已注册类型 T 的等号运算符注册至元对象系统,要求类型 T 具备等号运算符 operator==。若注册成功则返回 true,否则返回 false。
此函数在 Qt 5.5 中被引入。
[static] bool QMetaType::save(QDataStream &stream, int type, const void *data)
从数据流 stream 中读取对应 type 类型的对象至 data 中,若读取成功则返回 true,否则返回 false。
此类型必须在这之前通过 qRegisterMetaType() 和 qRegisterMetaTypeStreamOperators() 完成注册。
通常来说,您不需要显示调用此函数,而是应使用 QVariant 的 operator>>(),该运算符依赖 load() 来传递自定义类型。
另请参阅:save() 和 qRegisterMetaTypeStreamOperators()。
将 type 类型对应的 data 对象输出至数据流 stream 中,若读取成功则返回 true,否则返回 false。
此类型必须在这之前通过 qRegisterMetaType() 和 qRegisterMetaTypeStreamOperators() 完成注册。
通常来说,您不需要显示调用此函数,而是应使用 QVariant 的 operator<<(),该运算符依赖 save() 来传递自定义类型。
另请参阅:load() 和 qRegisterMetaTypeStreamOperators()。
[static] int QMetaType::sizeOf(int type)
返回 type 对应类型的以字节为单位的大小(即 sizeof(T),其中 T 是 type 对应的实际类型)。
此函数通常结合 construct() 使用,来进行对此类型的更底层的内存管理。
此函数在 Qt 5.0 中被引入。
另请参阅:construct()。
int QMetaType::sizeOf() const
返回此类型的以字节为单位的大小(即 sizeof(T),其中 T 是 QMetaType 对应的实际类型)。
此函数通常结合 construct() 使用,来进行对此类型的更底层的内存管理。
此函数在 Qt 5.0 中被引入。
另请参阅:QMetaType::construct() 和 QMetaType::sizeOf()。
[static] int QMetaType::type(const char *typeName)
返回名为 typeName 的类型的元类型编号,若无此元类型则返回 QMetaType::UnknownType。
另请参阅:isRegistered(),typeName() 和 Type。
[static] int QMetaType::type(const ::QByteArray &typeName)
这是一个重载函数。
返回名为 typeName 的类型的元类型编号,若无此元类型则返回 0(译者注:即QMetaType::UnknownType)。
此函数在 Qt 5.5 中被引入。
另请参阅:isRegistered() 和 typeName()。
[static] QMetaType::TypeFlags QMetaType::typeFlags(int type)
返回 type 类型的类型标志。
此函数在 Qt 5.0 中被引入。
另请参阅:QMetaType::TypeFlags。
[static] const char *QMetaType::typeName(int typeId)
返回 typeId 对应类型的类型名称,若该类型不存在则返回空指针。返回的指针不可被删除。
另请参阅:type(),isRegistered(),Type 和 name()。
相关非成员函数
template \ int qMetaTypeId()
返回类型 T 对应的元类型编号。若该类型未通过 Q_DECLARE_METATYPE() 声明,则会引发编译错误。
典型用法:
int id = qMetaTypeId<QString>(); // id 是 QMetaType::QStringid = qMetaTypeId<MyStruct>(); // 若 MyStruct 未被声明,则会产生编译错误
QMetaType::type() 返回值与 qMetaTypeId() 相同,但会基于类型名称进行运行时检索。QMetaType::type() 会稍慢一些,但即使类型未注册也能编译成功。
此函数在 Qt 4.1 中被引入。
另请参阅:Q_DECLARE_METATYPE() 和 QMetaType::type()。
template \ int qRegisterMetaType(const char *typeName)
将类型 T 通过类型名称 typeName 注册至元对象系统,并返回 QMetaType 使用的类型编号。任何包含一个公共默认构造函数、公共拷贝构造函数、公共析构函数的类或结构体均可被注册。
此函数要求类型 T 在此函数调用时被完整定义;对于指针类型,同样要求被指向的类型被完整定义(译者注:即不可为前置声明类型)。可以使用 Q_DECLARE_OPAQUE_POINTER() 来注册前置声明类型的指针类型。
类型被注册后,可以在运行时动态地创建或销毁对象。
下述为注册 MyClass 类的示例:
qRegisterMetaType<MyClass>("MyClass");
此函数可被用于注册类型别名,以便于将别名用于 QMetaProperty 或队列连接中。
typedef QString CustomString;qRegisterMetaType<CustomString>("CustomString");
警告: 此函数仅应被用于注册类型别名,其它场合请使用 Q_DECLARE_METATYPE 和 qMetaTypeId()。
另请参阅:qRegisterMetaTypeStreamOperators(),isRegistered() 和 Q_DECLARE_METATYPE()。
template \ int qRegisterMetaType()
调用此函数来注册类型 T。T 必须被 Q_DECLARE_METATYPE() 所声明。返回此类型对应的元类型编号。
示例:
int id = qRegisterMetaType<MyStruct>();
此函数要求类型 T 在此函数调用时被完整定义;对于指针类型,同样要求被指向的类型被完整定义(译者注:即不可为前置声明类型)。可以使用 Q_DECLARE_OPAQUE_POINTER() 来注册前置声明类型的指针类型。
类型被注册后,可以在运行时动态地创建或销毁对象。
为了在 QVariant 中使用类型 T,使用 Q_DECLARE_METATYPE() 便已足够。若要在信号槽的队列连接中使用 T,则 qRegisterMetaType<T>() 必须在第一个连接建立前被调用。
同样地,若要在 QObject::property() 中使用 T,qRegisterMetaType<T>() 必须在这之前被调用。通常在使用到 T 的类的构造函数中,或在 main() 函数中调用。
此函数在 Qt 4.2 中被引入。
另请参阅:Q_DECLARE_METATYPE()。
template \ void qRegisterMetaTypeStreamOperators(const char *typeName)
通过类型名称 typeName 将 T 的流运算符注册至元对象系统。
在此之后,该类型可通过 QMetaType::load() 和 QMetaType::save() 进行序列化和反序列化。这两个函数在将 QVariant 传递至数据流时被调用。
qRegisterMetaTypeStreamOperators<MyClass>("MyClass");
流运算符需要具有下述的函数签名:
QDataStream &operator<<(QDataStream &out, const MyClass &myObj);QDataStream &operator>>(QDataStream &in, MyClass &myObj);
另请参阅:qRegisterMetaType(),QMetaType::isRegistered() 和 Q_DECLARE_METATYPE()。
bool operator!=(const QMetaType &a, const QMetaType &b)
这是一个重载函数。
若 QMetaType a 的类型与 QMetaType b 不同则返回 true,否则返回false。
此函数在 Qt 5.15 中被引入。
bool operator==(const QMetaType &a, const QMetaType &b)
这是一个重载函数。
若 QMetaType a 的类型与 QMetaType b 相同则返回 true,否则返回false。
此函数在 Qt 5.15 中被引入。
宏定义文档
Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE(Container)
此宏令容器类型 Container 作为关联型容器被注册至 QMetaType,即允许将 Container<T, U> 实例存入 QVariant,前提是 T 和 U 也已经被注册为 QMetaType。
注意: 所有 Qt 的关联型容器已被内置支持,无需使用此宏进行声明。std::map 容器也已被内置支持。
下述代码展示了 Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE() 的典型用法:
#include <unordered_list>Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE(std::unordered_map)void someFunc(){std::unordered_map<int, bool> container;QVariant var = QVariant::fromValue(container);// ...}
译者注:
用户的自定义类型只需要通过
Q_DECLARE_METATYPE(T)注册后,即可被已注册的所有容器使用,无需再注册Q_DECLARE_METATYPE(QMap<QString, T>)。
Q_DECLARE_METATYPE(Type)
此宏将类型 Type 注册至 QMetaType ,前提是该类型具备一个公共默认构造函数、公共拷贝构造函数和公共析构函数。这是把类型 Type 用于 QVariant 的前提。
此宏要求类型 T 在此函数调用时被完整定义;对于指针类型,同样要求被指向的类型被完整定义(译者注:即不可为前置声明类型)。可以使用 Q_DECLARE_OPAQUE_POINTER() 来注册前置声明类型的指针类型。
理想情况下,此宏应被放置在该类型的声明位置之后。若不可行的话,也可以将其放置在一个私有头文件中,然后在每次在 QVariant 中使用此类型之前包含该头文件。
Q_DECLARE_METATYPE() 使此类型可被所有基于模板的函数使用,包括 QVariant 中的模板函数。注意,若想在信号槽的队列连接或 QObject 的属性系统中使用此类型,则还需要调用 qRegisterMetaType(),因为该类型的名称会在运行时被解析。
此示例为 Q_DECLARE_METATYPE() 的典型用法:
struct MyStruct{int i;...};Q_DECLARE_METATYPE(MyStruct)
若 MyStruct 处于命名空间中,则 Q_DECLARE_METATYPE() 宏必须在命令空间外使用:
namespace MyNamespace{...}Q_DECLARE_METATYPE(MyNamespace::MyStruct)
当 MyStruct 被注册至 QMetaType 后,便可将其用于 QVariant 中”
MyStruct s;QVariant var;var.setValue(s); // 将 v 拷贝至 QVariant...// 获取类型值MyStruct s2 = var.value<MyStruct>();
下述类型已被自动注册,无需使用此宏:
- 继承自 QObject 的类型的指针;
- QList\
,QVector\ ,QQueue\ ,QStack\ ,QSet\ 或 QLinkedList\ ,前提是 T被注册为元类型; - QHash\
,QMap\ 或 QPair\ ,前提是 T1和T2都被注册为元类型; - QPointer\
,QSharedPointer\ ,QWeakPointer\ ,前提是 T为 QObject 的子类; - 通过 Q_ENUM 或 Q_FLAG 注册的枚举类型;
- 包含 Q_GADGET 宏的类。
另请参阅:qRegisterMetaType()。
Q_DECLARE_OPAQUE_POINTER(PointerType)
此宏使得前置声明类型的指针类型 PointerType 可被 Q_DECLARE_METATYPE() 或 qRegisterMetaType() 注册至 QMetaType。
此函数在 Qt 5.0 中被引入。
另请参阅:Q_DECLARE_METATYPE() 和 qRegisterMetaType()。
Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(Container)
此宏令容器类型 Container 作为顺序容器被注册至 QMetaType,即允许将 Container<T> 实例存入 QVariant,前提是 T 已经被注册为 QMetaType。
注意: 所有 Qt 的顺序容器已被内置支持,无需使用此宏进行声明。std::vector 和 std::list 容器已也被内置支持。
下述代码展示了 Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE() 的典型用法:
#include <deque>Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(std::deque)void someFunc(){std::deque<QFile*> container;QVariant var = QVariant::fromValue(container);// ...}
译者注:
用户的自定义类型只需要通过
Q_DECLARE_METATYPE(T)注册后,即可被已注册的所有容器使用,无需再注册Q_DECLARE_METATYPE(QVector<T>)。
Q_DECLARE_SMART_POINTER_METATYPE(SmartPointer)
此宏令智能指针类型 SmartPointer 作为智能指针被注册至 QMetaType,即允许将 Container<T> 实例存入 QVariant,前提是 T 已经被注册为 QMetaType。
注意:QWeakPointer,QSharedPointer 和 QPointer 已被内置支持,无需使用此宏进行声明。
下述代码展示了 Q_DECLARE_SMART_POINTER_METATYPE() 的典型用法:
#include <memory>Q_DECLARE_SMART_POINTER_METATYPE(std::shared_ptr)void someFunc(){auto smart_ptr = std::make_shared<QFile>();QVariant var = QVariant::fromValue(smart_ptr);// ...if (var.canConvert<QObject*>()) {QObject *sp = var.value<QObject*>();qDebug() << sp->metaObject()->className(); // Prints 'QFile'.}}
译者注:
用户继承自 QObject 的自定义类型可直接被已注册的智能指针使用,无需再注册
Q_DECLARE_METATYPE(QSharedPointer<T>)。与容器不同的是,通过
Q_DECLARE_METATYPE(T)注册的自定义类型无法直接被已注册的智能指针使用,必须单独注册Q_DECLARE_METATYPE(QSharedPointer<T>)。
已废弃成员
QMetaType 类的以下成员已被废弃。它们仅为了保证老代码能够运行而保留,我们强烈反对在新代码中使用。
静态公共成员
| 返回类型 | 函数 |
|---|---|
(obsolete)void * |
construct(int type, const void *copy = nullptr) |
成员函数文档
[static] void *QMetaType::construct(int type, const void *copy = nullptr)
在给定的内存地址 where 上构造对应 type 类型的对象,该对象是 copy 的副本,并返回 where。若 copy 是空指针,则执行默认构造。
这是用于显示管理存储 type 类型对象的内存的底层函数。若不需要此类底层控制,则考虑使用 create() 函数(也就是指,使用 new 而非 placement new)。
您必须确保 where 指向的内存区域大小足够存储 type 对应的数据,并且 where 地址需要对齐,对应类型的大小可通过 sizeOf() 获取。
内存对齐的规则是对齐至类型的自然边界,也就是大于等于类型大小的2的n次方值,直至平台有效对齐宽度上限为止。对于特定用途来说,超过 2 * sizeof(void*) 的对齐宽度只是某些特定硬件指令所必需的(例如,x86 平台中对齐后的 SSE 读取和存储)。
此函数在 Qt 5.0 中被引入。
此函数已被废弃,仅为了保证老代码能够运行而保留,我们强烈反对在新代码中使用。
构造对应 type 类型的对象,该对象是 copy 的副本。copy 的默认值是 nullptr。
已弃用,该用新的静态函数 QMetaType::create(int type, const void *copy)。
