<QTypeInfo>

用于指定自定义类型信息的宏。更多

Header: #include <QTypeInfo>

Q_DECLARE_TYPEINFO(Type, Flags)

详细说明

宏文档

Q_DECLARE_TYPEINFO(Type, Flags)

您可以使用此宏来指定自定义类型Type 的相关信息。有了准确的类型信息,Qt 的通用容器就可以选择适当的存储方法和算法。

Flags 可以是以下内容之一:

  • Q_PRIMITIVE_TYPE 指定Type 无需执行任何操作即可正确销毁,可以使用 memcpy() 创建对象的有效独立副本。
  • Q_RELOCATABLE_TYPE 指定Type 有一个构造函数和/或析构函数,但仍可使用memcpy() 在内存中重新定位
  • Q_MOVABLE_TYPEQ_RELOCATABLE_TYPE 相同。倾向于在新代码中使用Q_RELOCATABLE_TYPE 。注意:尽管名称如此,但这与移动构造函数或 C++ 移动语义无关。
  • Q_COMPLEX_TYPE (默认情况下)指定Type 具有构造函数和/或析构函数,且不得在内存中移动。

原始 "类型示例:

struct Point2D
{
    int x;
    int y;
};

Q_DECLARE_TYPEINFO(Point2D, Q_PRIMITIVE_TYPE);

非 POD "原始 "类型的一个例子是QUuid :尽管QUuid 有构造函数(因此不是 POD),但每个位模式仍代表一个有效对象,而且 memcpy() 可用于创建QUuid 对象的有效独立副本。

可重置类型示例:

class Point2D
{
public:
    Point2D() { data = new int[2]; }
    Point2D(const Point2D &other) { ... }
    ~Point2D() { delete[] data; }

    Point2D &operator=(const Point2D &other) { ... }

    int x() const { return data[0]; }
    int y() const { return data[1]; }

private:
    int *data;
};

Q_DECLARE_TYPEINFO(Point2D, Q_RELOCATABLE_TYPE);

Qt 会尝试使用标准 C++ 类型特征检测类型的类别;使用此宏可以调整行为。例如,许多类型尽管并非完全可复制,但仍可使用 Q_RELOCATABLE_TYPE。

© 2025 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.