The QMetaType 類管理元對象係統中命名類型。 更多...
| 頭: | #include <QMetaType> |
注意: 此類的所有函數 綫程安全 .
| 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 ().
這些內置類型的支持通過 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 ().
[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 引入。
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.
注意: 此函數是 綫程安全 .
該函數在 Qt 4.1 引入。
另請參閱 Q_DECLARE_METATYPE () 和 QMetaType::type ().
注冊類型名稱
typeName
為類型
T
。返迴的內部 ID 用於
QMetaType
。可以注冊擁有公共默認構造函數、公共副本構造函數和公共析構函數的任何類 (或結構)。
在已注冊類型後,可以在運行時動態創建和銷毀該類型的對象。
此範例注冊類
MyClass
:
qRegisterMetaType<MyClass>("MyClass");
此函數對注冊 typedef 很有用,因此它們可以用於 QMetaProperty ,或在 QueuedConnections
typedef QString CustomString; qRegisterMetaType<CustomString>("CustomString");
注意: 此函數是 綫程安全 .
另請參閱 qRegisterMetaTypeStreamOperators (), QMetaType::isRegistered (),和 Q_DECLARE_METATYPE ().
調用此函數去注冊類型
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()
函數。
注意: 此函數是 綫程安全 .
該函數在 Qt 4.2 引入。
另請參閱 Q_DECLARE_METATYPE ().
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);
注意: 此函數是 綫程安全 .
另請參閱 qRegisterMetaType (), QMetaType::isRegistered (),和 Q_DECLARE_METATYPE ().
此宏使類型 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 ().