QMetaType 类

The QMetaType 类管理元对象系统中命名类型。 更多...

头: #include <QMetaType>

注意: 此类的所有函数 thread-safe .

公共类型

enum Type { Void, Bool, Int, UInt, ..., User }

静态公共成员

void * construct (int type , const void * copy = 0)
void destroy (int type , void * data )
bool isRegistered (int type )
bool load (QDataStream & stream , int type , void * data )
bool save (QDataStream & stream , int type , const void * data )
int type (const char * typeName )
const char * typeName (int type )
void unregisterType (const char * typeName )
int qMetaTypeId ()
int qRegisterMetaType (const char * typeName )
int qRegisterMetaType ()
void qRegisterMetaTypeStreamOperators (const char * typeName )

Q_DECLARE_METATYPE ( Type )

详细描述

The QMetaType 类管理元对象系统中命名类型。

用作帮助程序的类能编组类型在 QVariant 和在队列信号及槽连接中。它将类型名称关联到类型,以便可以在运行时动态创建和销毁它。声明新类型采用 Q_DECLARE_METATYPE () 以使它们可用于 QVariant 和其它基于模板的函数。调用 qRegisterMetaType () to make type available to non-template based functions, such as the queued signal and slot connections.

可以注册具有公共默认构造函数、公共副本构造函数及公共析构函数的任何类或结构。

以下代码分配并销毁实例化的 MyClass :

int id = QMetaType::type("MyClass");
if (id != 0) {
    void *myClassPtr = QMetaType::construct(id);
    ...
    QMetaType::destroy(id, myClassPtr);
    myClassPtr = 0;
}
					

若想要流运算符 operator<<() and operator>>() 工作在 QVariant 对象存储自定义类型,自定义类型必须提供 operator<<() and operator>>() 运算符。

另请参阅 Q_DECLARE_METATYPE (), QVariant::setValue (), QVariant::value (),和 QVariant::fromValue ().

成员类型文档编制

enum QMetaType:: Type

这些内置类型的支持通过 QMetaType :

常量 描述
QMetaType::Void 0 void
QMetaType::Bool 1 bool
QMetaType::Int 2 int
QMetaType::UInt 3 无符号 int
QMetaType::Double 6 double
QMetaType::QChar 7 QChar
QMetaType::QString 10 QString
QMetaType::QByteArray 12 QByteArray
QMetaType::VoidStar 128 void *
QMetaType::Long 129 long
QMetaType::LongLong 4 LongLong
QMetaType::Short 130 short
QMetaType::Char 131 char
QMetaType::ULong 132 unsigned long
QMetaType::ULongLong 5 ULongLong
QMetaType::UShort 133 unsigned short
QMetaType::UChar 134 unsigned char
QMetaType::Float 135 float
QMetaType::QObjectStar 136 QObject *
QMetaType::QWidgetStar 137 QWidget *
QMetaType::QVariant 138 QVariant
QMetaType::QColorGroup 63 QColorGroup
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::QColor 67 QColor
QMetaType::QSizeF 22 QSizeF
QMetaType::QRectF 20 QRectF
QMetaType::QLine 23 QLine
QMetaType::QTextLength 78 QTextLength
QMetaType::QStringList 11 QStringList
QMetaType::QVariantMap 8 QVariantMap
QMetaType::QVariantHash 28 QVariantHash
QMetaType::QIcon 69 QIcon
QMetaType::QPen 77 QPen
QMetaType::QLineF 24 QLineF
QMetaType::QTextFormat 79 QTextFormat
QMetaType::QRect 19 QRect
QMetaType::QPoint 25 QPoint
QMetaType::QUrl 17 QUrl
QMetaType::QRegExp 27 QRegExp
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 76 QKeySequence
QMetaType::QSizePolicy 75 QSizePolicy
QMetaType::QPixmap 65 QPixmap
QMetaType::QLocale 18 QLocale
QMetaType::QBitmap 73 QBitmap
QMetaType::QMatrix 80 QMatrix
QMetaType::QTransform 81 QTransform
QMetaType::QMatrix4x4 82 QMatrix4x4
QMetaType::QVector2D 83 QVector2D
QMetaType::QVector3D 84 QVector3D
QMetaType::QVector4D 85 QVector4D
QMetaType::QQuaternion 86 QQuaternion
QMetaType::QEasingCurve 29 QEasingCurve
QMetaType::User 256 用于用户类型的基值

可以注册额外类型,使用 Q_DECLARE_METATYPE ().

另请参阅 type () 和 typeName ().

成员函数文档编制

[static] void * QMetaType:: construct ( int type , const void * copy = 0)

Returns a copy of copy , assuming it is of type type 。若 copy is zero, creates a default type.

另请参阅 destroy (), isRegistered (),和 Type .

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

销毁 data , assuming it is of the type 给定。

另请参阅 construct (), isRegistered (),和 Type .

[static] bool QMetaType:: isRegistered ( int type )

Returns true if the datatype with ID type is registered; otherwise returns false.

另请参阅 type (), typeName (),和 Type .

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

Reads the object of the specified type 从给定 stream into data . Returns true if the object is loaded successfully; otherwise returns false.

The type must have been registered with qRegisterMetaType () 和 qRegisterMetaTypeStreamOperators () 事先。

Normally, you should not need to call this function directly. Instead, use QVariant 's operator>>() , which relies on load() to stream custom types.

另请参阅 save () 和 qRegisterMetaTypeStreamOperators ().

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

Writes the object pointed to by data with the ID type 到给定 stream . Returns true if the object is saved successfully; otherwise returns false.

The type must have been registered with qRegisterMetaType () 和 qRegisterMetaTypeStreamOperators () 事先。

Normally, you should not need to call this function directly. Instead, use QVariant 's operator<<() , which relies on save() to stream custom types.

另请参阅 load () 和 qRegisterMetaTypeStreamOperators ().

[static] int QMetaType:: type (const char * typeName )

将句柄返回给类型调用 typeName ,或 0 若没有这种类型。

另请参阅 isRegistered (), typeName (),和 Type .

[static] const char * QMetaType:: typeName ( int type )

Returns the type name associated with the given type , or 0 if no matching type was found. The returned pointer must not be deleted.

另请参阅 type (), isRegistered (),和 Type .

[static] void QMetaType:: unregisterType (const char * typeName )

Unregisters a user type, with typeName .

该函数在 Qt 4.4 引入。

另请参阅 type () 和 typeName ().

相关非成员

int qMetaTypeId ()

Returns the meta type id of type T at compile time. If the type was not declared with Q_DECLARE_METATYPE (), compilation will fail.

典型用法:

int id = qMetaTypeId<QString>();    // id is now QMetaType::QString
id = qMetaTypeId<MyStruct>();       // compile error if MyStruct not declared
					

QMetaType::type () returns the same ID as qMetaTypeId(), but does a lookup at runtime based on the name of the type. QMetaType::type () is a bit slower, but compilation succeeds if a type is not registered.

注意: 此函数是 thread-safe .

该函数在 Qt 4.1 引入。

另请参阅 Q_DECLARE_METATYPE () 和 QMetaType::type ().

int qRegisterMetaType (const char * typeName )

注册类型名称 typeName 为类型 T 。返回的内部 ID 用于 QMetaType 。可以注册拥有公共默认构造函数、公共副本构造函数和公共析构函数的任何类 (或结构)。

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

此范例注册类 MyClass :

qRegisterMetaType<MyClass>("MyClass");
					

此函数对注册 typedef 很有用,因此它们可以用于 QMetaProperty ,或在 QueuedConnections

typedef QString CustomString;
qRegisterMetaType<CustomString>("CustomString");
					

注意: 此函数是 thread-safe .

另请参阅 qRegisterMetaTypeStreamOperators (), QMetaType::isRegistered (),和 Q_DECLARE_METATYPE ().

int qRegisterMetaType ()

调用此函数去注册类型 T . T 必须被声明采用 Q_DECLARE_METATYPE ()。返回元类型 ID。

范例:

int id = qRegisterMetaType<MyStruct>();
					

要使用类型 T in QVariant ,使用 Q_DECLARE_METATYPE () is sufficient. To use the type T in queued signal and slot connections, qRegisterMetaType<T>() must be called before the first connection is established.

Also, to use type T 采用 QObject::property () API, qRegisterMetaType<T>() must be called before it is used, typically in the constructor of the class that uses T , or in the main() 函数。

注意: 此函数是 thread-safe .

该函数在 Qt 4.2 引入。

另请参阅 Q_DECLARE_METATYPE ().

void qRegisterMetaTypeStreamOperators (const char * typeName )

Registers the stream operators for the type T called typeName .

Afterward, the type can be streamed using QMetaType::load () 和 QMetaType::save (). These functions are used when streaming a QVariant .

qRegisterMetaTypeStreamOperators<MyClass>("MyClass");
					

The stream operators should have the following signatures:

QDataStream &operator<<(QDataStream &out, const MyClass &myObj);
QDataStream &operator>>(QDataStream &in, MyClass &myObj);
					

注意: 此函数是 thread-safe .

另请参阅 qRegisterMetaType (), QMetaType::isRegistered (),和 Q_DECLARE_METATYPE ().

宏文档编制

Q_DECLARE_METATYPE ( Type )

此宏使类型 Type 知道 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 .

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

此范例展示 Q_DECLARE_METATYPE() 的典型用例:

struct MyStruct
{
    int i;
    ...
};
Q_DECLARE_METATYPE(MyStruct)
					

MyStruct is in a namespace, the Q_DECLARE_METATYPE() macro has to be outside the namespace:

namespace MyNamespace
{
    ...
}
Q_DECLARE_METATYPE(MyNamespace::MyStruct)
					

由于 MyStruct 现在知道 QMetaType ,它可以用于 QVariant :

MyStruct s;
QVariant var;
var.setValue(s); // copy s into the variant
...
// retrieve the value
MyStruct s2 = var.value<MyStruct>();
					

另请参阅 qRegisterMetaType ().