Obsolete Members for QMetaType

QMetaType 类的以下成员已被废弃。提供这些成员是为了确保旧源代码能够继续运行。我们强烈建议不要在新代码中使用它们。

静态公共成员

(deprecated in 6.0) bool compare(const void *lhs, const void *rhs, int typeId, int *result)
(deprecated in 6.0) void *construct(int type, void *where, const void *copy)
(deprecated in 6.0) bool convert(const void *from, int fromTypeId, void *to, int toTypeId)
(deprecated in 6.0) void *create(int type, const void *copy = nullptr)
(deprecated in 6.0) bool debugStream(QDebug &dbg, const void *rhs, int typeId)
(deprecated in 6.0) void destroy(int type, void *data)
(deprecated in 6.0) void destruct(int type, void *where)
(deprecated in 6.0) bool hasRegisteredDebugStreamOperator()
(deprecated in 6.0) bool hasRegisteredDebugStreamOperator(int typeId)
(deprecated in 6.0) bool load(QDataStream &stream, int type, void *data)
(deprecated in 6.0) const QMetaObject *metaObjectForType(int type)
(deprecated in 6.0) bool save(QDataStream &stream, int type, const void *data)
(deprecated in 6.0) int sizeOf(int type)
(deprecated in 6.0) int type(const char *typeName)
(deprecated in 6.0) int type(const QByteArray &typeName)
(deprecated in 6.0) QMetaType::TypeFlags typeFlags(int type)
(deprecated in 6.0) const char *typeName(int typeId)

相关的非成员

(deprecated) int qRegisterMetaType(const char *typeName)

成员函数文档

[static, deprecated in 6.0] bool QMetaType::compare(const void *lhs, const void *rhs, int typeId, int *result)

自 6.0 版本起,此函数已被废弃。我们强烈建议不要在新代码中使用它。

请改用非静态的 compare 方法。

比较位于lhs 和rhs 处的两个对象。这两个对象必须均为typeId 类型。若lhs 小于、等于或大于rhs ,则result 被设置为小于、等于或大于零。若比较成功,则返回true ;否则返回false 。

[static, deprecated in 6.0] void *QMetaType::construct(int type, void *where, const void *copy)

自 6.0 版本起,此函数已被废弃。我们强烈建议不要在新代码中使用它。

在由where 所指向的现有内存中构造给定type 类型的值,该值是copy 的副本,并返回where 。如果copy 为零,则对该值进行默认构造。

这是一个用于显式管理该类型存储内存的底层函数。若您不需要此级别的控制,请考虑调用create()(即使用“new”而非“placement new”)。

您必须确保where 指向一个能够存储type 类型值的地址,并且where 已进行适当的对齐。可以通过调用sizeOf() 查询该类型的大小。

对齐的经验法则是:将类型对齐到其自然边界,即大于该类型的最小 2 的幂,除非该对齐大小超过该平台的最大有效对齐值。 从实际应用来看,只有在涉及特殊硬件指令(例如 x86 架构上对齐的 SSE 加载和存储指令)时,才需要比 2 * sizeof(void*) 更大的对齐度。

另请参阅 destruct() 和sizeOf()。

[static, deprecated in 6.0] bool QMetaType::convert(const void *from, int fromTypeId, void *to, int toTypeId)

自 6.0 起,此函数已弃用。我们强烈建议不要在新代码中使用它。

将位于from 的对象从fromTypeId 转换为位于to 的预分配空间,该空间的类型为toTypeId 。若转换成功,则返回true ;否则返回 false。

from 和to 必须都是有效的指针。

[static, deprecated in 6.0] void *QMetaType::create(int type, const void *copy = nullptr)

该函数自 6.0 起已弃用。我们强烈建议不要在新代码中使用它。

返回 `copy` 的副本,前提是其类型为 `type`。如果 `copy ` 为零,则创建一个默认构造的实例。

另请参阅 destroy()、isRegistered() 和Type 。

[static, deprecated in 6.0] bool QMetaType::debugStream(QDebug &dbg, const void *rhs, int typeId)

自 6.0 版本起,此函数已被废弃。我们强烈建议不要在新代码中使用它。

这是一个重载函数。

[static, deprecated in 6.0] void QMetaType::destroy(int type, void *data)

自 6.0 起,此函数已被废弃。我们强烈建议不要在新代码中使用它。

销毁data 对象,前提是该对象属于给定的type 类型。

另请参阅 create()、isRegistered() 以及Type 。

[static, deprecated in 6.0] void QMetaType::destruct(int type, void *where)

该函数自 6.0 版本起已弃用。我们强烈建议不要在新代码中使用它。

销毁位于 `where` 处的给定 `type` 的值。

与destroy()不同,此函数仅调用该类型的析构函数,不会调用delete运算符。

另请参阅 construct()。

[static, deprecated in 6.0] template <typename T> bool QMetaType::hasRegisteredDebugStreamOperator()

自 6.0 版本起,此函数已被废弃。我们强烈建议在新代码中不要使用它。

如果元类型系统为类型 T 注册了调试流运算符,则返回true 。

[static, deprecated in 6.0] bool QMetaType::hasRegisteredDebugStreamOperator(int typeId)

自 6.0 版本起,此函数已被废弃。我们强烈建议不要在新代码中使用它。

请改用 `QMetaType::hasRegisteredDebugStreamOperator()`。

如果元类型系统为类型 IDtypeId 注册了调试流运算符,则返回 `true`。

[static, deprecated in 6.0] bool QMetaType::load(QDataStream &stream, int type, void *data)

自 6.0 起,此函数已被废弃。我们强烈建议在新代码中不要使用它。

这是一个重载函数。

[static, deprecated in 6.0] const QMetaObject *QMetaType::metaObjectForType(int type)

自 6.0 版本起,此函数已被废弃。我们强烈建议不要在新代码中使用它。

返回QMetaType::metaObject ,用于type

另请参阅 ` metaObject()`。

[static, deprecated in 6.0] bool QMetaType::save(QDataStream &stream, int type, const void *data)

自 6.0 版本起,此函数已被废弃。我们强烈建议不要在新代码中使用它。

这是一个重载函数。

[static, deprecated in 6.0] int QMetaType::sizeOf(int type)

自 6.0 起,此函数已弃用。我们强烈建议不要在新代码中使用它。

返回给定 `type ` 的大小(以字节为单位,即 `sizeof(T)`,其中 `T` 是由 `type ` 参数标识的实际类型)。

该函数通常与 `construct()` 配合使用,以对某种类型所占用的内存进行低级管理。

另请参阅 construct() 和QMetaType::alignOf()。

[static, deprecated in 6.0] int QMetaType::type(const char *typeName)

自 6.0 版本起,该函数已被废弃。我们强烈建议不要在新代码中使用它。

返回名为typeName 的类型的句柄;如果不存在该类型,则返回QMetaType::UnknownType 。

另请参阅 isRegistered()、typeName() 以及Type 。

[static, deprecated in 6.0] int QMetaType::type(const QByteArray &typeName)

自 6.0 版本起,此函数已被废弃。我们强烈建议不要在新代码中使用它。

返回名为typeName 的类型的句柄,如果不存在此类,则返回0。

这是一个重载函数。

另请参阅 isRegistered() 和typeName()。

[static, deprecated in 6.0] QMetaType::TypeFlags QMetaType::typeFlags(int type)

自 6.0 起,此函数已弃用。我们强烈建议在新代码中不要使用它。

返回给定 `type` 的标志。

另请参阅 QMetaType::TypeFlags 。

[static, deprecated in 6.0] const char *QMetaType::typeName(int typeId)

自 6.0 版本起,此函数已被废弃。我们强烈建议不要在新代码中使用它。

返回与给定的typeId 关联的类型名称;若未找到匹配的类型,则返回空指针。返回的指针不得被删除。

另请参见 type()、isRegistered()、Type 以及name()。

相关的非成员函数

[deprecated] template <typename T> int qRegisterMetaType(const char *typeName)

此函数已弃用。我们强烈建议不要在新代码中使用它。

为类型T 注册类型名称typeName 。返回QMetaType 所使用的内部 ID。任何具有公共默认构造函数、公共复制构造函数和公共析构函数的类或结构体均可进行注册。

该函数要求在调用时,T 必须是一个已完全定义的类型。对于指针类型,还要求被指向的类型已完全定义。请使用Q_DECLARE_OPAQUE_POINTER() 来注册对前向声明类型的指针。

类型注册完成后,您可以在运行时动态地创建和销毁该类型的对象。

以下示例注册了类MyClass :

qRegisterMetaType<MyClass>("MyClass");

此函数可用于注册类型别名,以便QMetaProperty 或QueuedConnections能够使用它们

typedefQString CustomString;
qRegisterMetaType<CustomString>("CustomString");

警告:此 函数仅适用于注册别名(typedef),对于其他所有用例,应改用Q_DECLARE_METATYPE 和qMetaTypeId()。

另请参阅 isRegistered() 和Q_DECLARE_METATYPE()。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.