The QMetaType 類管理元對象係統中命名類型。 更多...
| 頭: | #include <QMetaType> |
| qmake: | QT += core |
注意: 此類的所有函數 綫程安全 .
| enum | Type { Void, Bool, Int, UInt, ..., UnknownType } |
| enum | TypeFlag { NeedsConstruction, NeedsDestruction, MovableType, IsEnumeration, PointerToQObject } |
| flags | TypeFlags |
| typedef | TypedConstructor |
| typedef | TypedDestructor |
| QMetaType (const int typeId ) | |
| ~QMetaType () | |
| void * | construct (void * where , const void * copy = ...) const |
| void * | create (const void * copy = ...) const |
| void | destroy (void * data ) const |
| void | destruct (void * data ) const |
| QMetaType::TypeFlags | flags () const |
| bool | isRegistered () const |
| bool | isValid () const |
| const QMetaObject * | metaObject () 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 ) |
| 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 ) |
| Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE ( 容器 ) | |
| Q_DECLARE_METATYPE ( Type ) | |
| Q_DECLARE_OPAQUE_POINTER ( PointerType ) | |
| Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE ( 容器 ) | |
| Q_DECLARE_SMART_POINTER_METATYPE ( SmartPointer ) |
The QMetaType 類管理元對象係統中命名類型。
用作幫助程序的類能編組類型在 QVariant 和在隊列信號及槽連接中。它將類型名稱關聯到類型,以便可以在運行時動態創建和銷毀它。聲明新類型采用 Q_DECLARE_METATYPE () 以使它們可用於 QVariant 和其它基於模闆的函數。調用 qRegisterMetaType () 以使類型可用於基於非模闆的函數,譬如:隊列信號和槽連接。
可以注冊具有公共默認構造函數、公共副本構造函數及公共析構函數的任何類或結構。
以下代碼分配並銷毀實例化的
MyClass
:
int id = QMetaType::type("MyClass"); if (id != QMetaType::UnknownType) { void *myClassPtr = QMetaType::create(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
|
43
|
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::Nullptr
|
51
|
std::nullptr_t
|
QMetaType::VoidStar
|
31
|
void *
|
QMetaType::Long
|
32
|
long
|
QMetaType::LongLong
|
4
|
LongLong |
QMetaType::Short
|
33
|
short
|
QMetaType::Char
|
34
|
char
|
QMetaType::ULong
|
35
|
unsigned long
|
QMetaType::ULongLong
|
5
|
ULongLong |
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::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::QModelIndex
|
42
|
QModelIndex |
QMetaType::QPersistentModelIndex
|
50
|
QPersistentModelIndex (從 5.5 起) |
QMetaType::QUuid
|
30
|
QUuid |
QMetaType::QByteArrayList
|
49
|
QByteArrayList |
QMetaType::User
|
1024
|
用於用戶類型的基值 |
QMetaType::UnknownType
|
0
|
這是無效類型 ID。返迴它從 QMetaType 為未注冊類型 |
可以注冊額外類型,使用 Q_DECLARE_METATYPE ().
The enum describes attributes of a type supported by QMetaType .
| 常量 | 值 | 描述 |
|---|---|---|
QMetaType::NeedsConstruction
|
0x1
|
This type has non-trivial constructors. If the flag is not set instances can be safely initialized with memset to 0. |
QMetaType::NeedsDestruction
|
0x2
|
This type has a non-trivial destructor. If the flag is not set calls to the destructor are not necessary before discarding objects. |
QMetaType::MovableType
|
0x4
|
An instance of a type having this attribute can be safely moved by memcpy. |
QMetaType::IsEnumeration
|
0x10
|
This type is an enumeration |
QMetaType::PointerToQObject
|
0x8
|
This type is a pointer to a derived of QObject |
TypeFlags 類型是 typedef 對於 QFlags <TypeFlag>。它存儲 TypeFlag 值的 OR (或) 組閤。
構造 QMetaType object that contains all information about type typeId .
該函數在 Qt 5.0 引入。
Destructs this object.
[static]
bool
QMetaType::
compare
(const
void
*
lhs
, const
void
*
rhs
,
int
typeId
,
int
*
result
)
比較對象在
lhs
and
rhs
. Both objects need to be of type
typeId
.
result
is set to less than, equal to or greater than zero, if
lhs
is less than, equal to or greater than
rhs
。返迴
true
, if the comparison succeeded, otherwise
false
.
該函數在 Qt 5.2 引入。
[static]
void
*QMetaType::
construct
(
int
type
,
void
*
where
, const
void
*
copy
)
Constructs a value of the given type in the existing memory addressed by where , that is a copy of copy ,並返迴 where 。若 copy is zero, the value is default constructed.
This is a low-level function for explicitly managing the memory used to store the type. Consider calling create () if you don't need this level of control (that is, use "new" rather than "placement new").
You must ensure that where points to a location that can store a value of type type , and that where is suitably aligned. The type's size can be queried by calling sizeOf ().
The rule of thumb for alignment is that a type is aligned to its natural boundary, which is the smallest power of 2 that is bigger than the type, unless that alignment is larger than the maximum useful alignment for the platform. For practical purposes, alignment larger than 2 * sizeof(void*) is only necessary for special hardware instructions (e.g., aligned SSE loads and stores on x86).
該函數在 Qt 5.0 引入。
Constructs a value of the type that this QMetaType instance was constructed for in the existing memory addressed by where , that is a copy of copy ,並返迴 where 。若 copy is zero, the value is default constructed.
This is a low-level function for explicitly managing the memory used to store the type. Consider calling create () if you don't need this level of control (that is, use "new" rather than "placement new").
You must ensure that where points to a location where the new value can be stored and that where is suitably aligned. The type's size can be queried by calling sizeOf ().
The rule of thumb for alignment is that a type is aligned to its natural boundary, which is the smallest power of 2 that is bigger than the type, unless that alignment is larger than the maximum useful alignment for the platform. For practical purposes, alignment larger than 2 * sizeof(void*) is only necessary for special hardware instructions (e.g., aligned SSE loads and stores on x86).
該函數在 Qt 5.0 引入。
[static]
bool
QMetaType::
convert
(const
void
*
from
,
int
fromTypeId
,
void
*
to
,
int
toTypeId
)
Converts the object at
from
from
fromTypeId
to the preallocated space at
to
typed
toTypeId
。返迴
true
,若轉換成功,否則 false。
該函數在 Qt 5.2 引入。
[static]
void
*QMetaType::
create
(
int
type
, const
void
*
copy
= nullptr)
Returns a copy of copy , assuming it is of type type 。若 copy is zero, creates a default constructed instance.
另請參閱 destroy (), isRegistered (),和 Type .
Returns a copy of copy , assuming it is of the type that this QMetaType instance was created for. If copy is null, creates a default constructed instance.
該函數在 Qt 5.0 引入。
另請參閱 QMetaType::destroy ().
[static]
bool
QMetaType::
debugStream
(
QDebug
&
dbg
, const
void
*
rhs
,
int
typeId
)
Streams the object at
rhs
類型
typeId
to the debug stream
dbg
。返迴
true
當成功時,否則 false。
該函數在 Qt 5.2 引入。
[static]
void
QMetaType::
destroy
(
int
type
,
void
*
data
)
銷毀 data , assuming it is of the type 給定。
另請參閱 create (), isRegistered (),和 Type .
銷毀 data , assuming it is of the type that this QMetaType instance was created for.
該函數在 Qt 5.0 引入。
另請參閱 QMetaType::create ().
[static]
void
QMetaType::
destruct
(
int
type
,
void
*
where
)
Destructs the value of the given type , located at where .
不像 destroy (), this function only invokes the type's destructor, it doesn't invoke the delete operator.
該函數在 Qt 5.0 引入。
另請參閱 construct ().
Destructs the value, located at data , assuming that it is of the type for which this QMetaType instance was constructed for.
不像 destroy (), this function only invokes the type's destructor, it doesn't invoke the delete operator.
該函數在 Qt 5.0 引入。
另請參閱 QMetaType::construct ().
[static]
bool
QMetaType::
等於
(const
void
*
lhs
, const
void
*
rhs
,
int
typeId
,
int
*
result
)
比較對象在
lhs
and
rhs
. Both objects need to be of type
typeId
.
result
is set to zero, if
lhs
equals to rhs. Returns
true
, if the comparison succeeded, otherwise
false
.
該函數在 Qt 5.5 引入。
Returns flags of the type for which this QMetaType instance was constructed.
該函數在 Qt 5.0 引入。
另請參閱 QMetaType::TypeFlags and QMetaType::typeFlags ().
[static]
bool
QMetaType::
hasRegisteredComparators
()
返迴
true
, if the meta type system has registered comparators for type T.
該函數在 Qt 5.2 引入。
[static]
bool
QMetaType::
hasRegisteredComparators
(
int
typeId
)
返迴
true
, if the meta type system has registered comparators for type id
typeId
.
該函數在 Qt 5.2 引入。
[static]
bool
QMetaType::
hasRegisteredConverterFunction
(
int
fromTypeId
,
int
toTypeId
)
返迴
true
, if the meta type system has a registered conversion from meta type id
fromTypeId
to
toTypeId
該函數在 Qt 5.2 引入。
[static]
bool
QMetaType::
hasRegisteredConverterFunction
()
返迴
true
, if the meta type system has a registered conversion from type From to type To.
這是重載函數。
該函數在 Qt 5.2 引入。
[static]
bool
QMetaType::
hasRegisteredDebugStreamOperator
()
返迴
true
, if the meta type system has a registered debug stream operator for type T.
該函數在 Qt 5.2 引入。
[static]
bool
QMetaType::
hasRegisteredDebugStreamOperator
(
int
typeId
)
返迴
true
, if the meta type system has a registered debug stream operator for type id
typeId
.
該函數在 Qt 5.2 引入。
[static]
bool
QMetaType::
isRegistered
(
int
type
)
返迴
true
if the datatype with ID
type
is registered; otherwise returns
false
.
另請參閱 type (), typeName (),和 Type .
返迴
true
若此
QMetaType
object contains valid information about a type, false otherwise.
該函數在 Qt 5.0 引入。
返迴
true
若此
QMetaType
object contains valid information about a type, false otherwise.
該函數在 Qt 5.0 引入。
[static]
bool
QMetaType::
load
(
QDataStream
&
stream
,
int
type
,
void
*
data
)
Reads the object of the specified
type
從給定
stream
into
data
。返迴
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 ().
return a QMetaObject relative to this type.
If the type is a pointer type to a subclass of QObject , flags () contains QMetaType::PointerToQObject and this function returns the corresponding QMetaObject . This can be used to in combinaison with QMetaObject::construct to create QObject of this type.
If the type is a Q_GADGET , flags () contains QMetaType::IsGadget , and this function returns its QMetaObject . This can be used to retrieve QMetaMethod and QMetaProperty and use them on a pointer of this type. (given by QVariant::data for example)
If the type is an enumeration, flags () contains QMetaType::IsEnumeration , and this function returns the QMetaObject of the enclosing object if the enum was registered as a Q_ENUM or 0 otherwise
該函數在 Qt 5.5 引入。
另請參閱 QMetaType::metaObjectForType () 和 QMetaType::flags ().
[static]
const
QMetaObject
*QMetaType::
metaObjectForType
(
int
type
)
返迴 QMetaType::metaObject for type
該函數在 Qt 5.0 引入。
另請參閱 metaObject ().
[static]
bool
QMetaType::
registerComparators
()
Registers comparison operators for the user-registered type T. This requires T to have both an operator== and an operator<. Returns
true
if the registration succeeded, otherwise false.
該函數在 Qt 5.2 引入。
[static]
bool
QMetaType::
registerConverter
()
Registers the possibility of an implicit conversion from type From to type To in the meta type system. Returns
true
if the registration succeeded, otherwise false.
該函數在 Qt 5.2 引入。
[static]
bool
QMetaType::
registerConverter
(
MemberFunction
function
)
這是重載函數。
Registers a method
function
like To From::function() const as converter from type From to type To in the meta type system. Returns
true
if the registration succeeded, otherwise false.
該函數在 Qt 5.2 引入。
[static]
bool
QMetaType::
registerConverter
(
MemberFunctionOk
function
)
這是重載函數。
Registers a method
function
like To From::function(bool *ok) const as converter from type From to type To in the meta type system. Returns
true
if the registration succeeded, otherwise false.
該函數在 Qt 5.2 引入。
[static]
bool
QMetaType::
registerConverter
(
UnaryFunction
function
)
這是重載函數。
Registers a unary function object
function
as converter from type From to type To in the meta type system. Returns
true
if the registration succeeded, otherwise false.
該函數在 Qt 5.2 引入。
[static]
bool
QMetaType::
registerDebugStreamOperator
()
Registers the debug stream operator for the user-registered type T. This requires T to have an operator<<(
QDebug
dbg, T). Returns
true
if the registration succeeded, otherwise false.
[static]
bool
QMetaType::
registerEqualsComparator
()
Registers equals operator for the user-registered type T. This requires T to have an operator==. Returns
true
if the registration succeeded, otherwise false.
該函數在 Qt 5.5 引入。
[static]
bool
QMetaType::
save
(
QDataStream
&
stream
,
int
type
, const
void
*
data
)
Writes the object pointed to by
data
with the ID
type
到給定
stream
。返迴
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::
sizeOf
(
int
type
)
Returns the size of the given type in bytes (i.e. sizeof(T), where T is the actual type identified by the type argument).
This function is typically used together with construct () to perform low-level management of the memory used by a type.
該函數在 Qt 5.0 引入。
另請參閱 construct ().
Returns the size of the type in bytes (i.e. sizeof(T), where T is the actual type for which this QMetaType instance was constructed for).
This function is typically used together with construct () to perform low-level management of the memory used by a type.
該函數在 Qt 5.0 引入。
另請參閱 QMetaType::construct () and QMetaType::sizeOf().
[static]
int
QMetaType::
type
(const
char
*
typeName
)
將句柄返迴給類型調用 typeName ,或 QMetaType::UnknownType 若沒有這種類型。
另請參閱 isRegistered (), typeName (),和 Type .
[static]
int
QMetaType::
type
(const
::QByteArray
&
typeName
)
這是重載函數。
將句柄返迴給類型調用 typeName ,或 0 若沒有這種類型。
該函數在 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
)
Returns the type name associated with the given typeId , or a null pointer if no matching type was found. The returned pointer must not be deleted.
另請參閱 type (), isRegistered (),和 Type .
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
。可以注冊擁有公共默認構造函數、公共副本構造函數和公共析構函數的任何類 (或結構)。
此函數要求
T
是在函數被調用時的完全定義類型。對於指針類型,它還要求指嚮完全定義的類型。使用
Q_DECLARE_OPAQUE_POINTER
() 能夠注冊指嚮轉發聲明類型的指針。
在已注冊類型後,可以在運行時動態創建和銷毀該類型的對象。
此範例注冊類
MyClass
:
qRegisterMetaType<MyClass>("MyClass");
此函數對注冊 typedef 很有用,因此它們可以用於 QMetaProperty ,或在 QueuedConnections
typedef QString CustomString; qRegisterMetaType<CustomString>("CustomString");
警告: 此函數僅適用於注冊彆名 (typedef) 為每種其它用例 Q_DECLARE_METATYPE and qMetaTypeId () 應該被使用以取而代之。
注意: 此函數是 綫程安全 .
另請參閱 qRegisterMetaTypeStreamOperators (), isRegistered (),和 Q_DECLARE_METATYPE ().
調用此函數去注冊類型
T
.
T
必須被聲明采用
Q_DECLARE_METATYPE
()。返迴元類型 ID。
範例:
int id = qRegisterMetaType<MyStruct>();
此函數要求
T
是在函數被調用時的完全定義類型。對於指針類型,它還要求指嚮完全定義的類型。使用
Q_DECLARE_OPAQUE_POINTER
() 能夠注冊指嚮轉發聲明類型的指針。
在已注冊類型後,可以在運行時動態創建和銷毀該類型的對象。
要使用類型
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 ().
This macro makes the container 容器 知道 QMetaType as an associative container. This makes it possible to put an instance of Container<T, U> into a QVariant , if T and U are themselves known to QMetaType .
Note that all of the Qt associative containers already have built-in support, and it is not necessary to use this macro with them. The std::map container also has built-in support.
This example shows a typical use of 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); // ... }
此宏使類型 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 .
此宏要求 Type is a fully defined type at the point where it is used. For pointer types, it also requires that the pointed to type is fully defined. Use in conjunction with Q_DECLARE_OPAQUE_POINTER () to register pointers to forward declared types.
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>();
Some types are registered automatically and do not need this macro:
另請參閱 qRegisterMetaType ().
This macro enables pointers to forward-declared types ( PointerType ) to be registered with QMetaType 使用 Q_DECLARE_METATYPE () 或 qRegisterMetaType ().
該函數在 Qt 5.0 引入。
另請參閱 Q_DECLARE_METATYPE () 和 qRegisterMetaType ().
This macro makes the container 容器 知道 QMetaType as a sequential container. This makes it possible to put an instance of Container<T> into a QVariant , if T itself is known to QMetaType .
Note that all of the Qt sequential containers already have built-in support, and it is not necessary to use this macro with them. The std::vector and std::list containers also have built-in support.
This example shows a typical use of 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); // ... }
此宏使智能指針 SmartPointer 知道 QMetaType as a smart pointer. This makes it possible to put an instance of SmartPointer<T> into a QVariant , if T is a type which inherits QObject .
注意, QWeakPointer , QSharedPointer and QPointer already have built-in support, and it is not necessary to use this macro with them.
This example shows a typical use of 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'. } }