Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QMetaType Class Reference

\inmodule QtCore More...

#include <qmetatype.h>

Collaboration diagram for QMetaType:

Public Types

enum  Type {
  FirstCoreType = Bool , LastCoreType = Float16 , FirstGuiType = QFont , LastGuiType = QColorSpace ,
  FirstWidgetsType = QSizePolicy , LastWidgetsType = QSizePolicy , HighestInternalId = LastWidgetsType , QReal = sizeof(qreal) == sizeof(double) ? Double : Float ,
  UnknownType = 0 , User = 65536
}
 \macro Q_DECLARE_OPAQUE_POINTER(PointerType) More...
enum  TypeFlag {
  NeedsConstruction = 0x1 , NeedsDestruction = 0x2 , RelocatableType = 0x4 , PointerToQObject = 0x8 ,
  IsEnumeration = 0x10 , SharedPointerToQObject = 0x20 , WeakPointerToQObject = 0x40 , TrackingPointerToQObject = 0x80 ,
  IsUnsignedEnumeration = 0x100 , IsGadget = 0x200 , PointerToGadget = 0x400 , IsPointer = 0x800 ,
  IsQmlList =0x1000 , IsConst = 0x2000 , NeedsCopyConstruction = 0x4000 , NeedsMoveConstruction = 0x8000
}
 The enum describes attributes of a type supported by QMetaType. More...
using ConverterFunction = std::function<bool(const void *src, void *target)>
using MutableViewFunction = std::function<bool(void *src, void *target)>

Public Member Functions

 QMetaType (int type)
constexpr QMetaType (const QtPrivate::QMetaTypeInterface *d)
constexpr QMetaType ()=default
constexpr bool isValid (QT6_DECL_NEW_OVERLOAD) const noexcept
bool isRegistered (QT6_DECL_NEW_OVERLOAD) const noexcept
void registerType () const
int id (int=0) const
constexpr qsizetype sizeOf () const
constexpr qsizetype alignOf () const
constexpr TypeFlags flags () const
constexpr const QMetaObjectmetaObject () const
constexpr const char * name () const
voidcreate (const void *copy=nullptr) const
void destroy (void *data) const
voidconstruct (void *where, const void *copy=nullptr) const
void destruct (void *data) const
QPartialOrdering compare (const void *lhs, const void *rhs) const
 Compares the objects at lhs and rhs for ordering.
bool equals (const void *lhs, const void *rhs) const
 Compares the objects at lhs and rhs for equality.
bool isDefaultConstructible () const noexcept
bool isCopyConstructible () const noexcept
bool isMoveConstructible () const noexcept
bool isDestructible () const noexcept
bool isEqualityComparable () const
 Returns true if a less than or equality operator for the type described by this metatype was visible to the metatype declaration, otherwise false.
bool isOrdered () const
 Returns true if a less than operator for the type described by this metatype was visible to the metatype declaration, otherwise false.
bool save (QDataStream &stream, const void *data) const
 Writes the object pointed to by data to the given stream.
bool load (QDataStream &stream, void *data) const
 Reads the object of this type from the given stream into data.
bool hasRegisteredDataStreamOperators () const
QMetaType underlyingType () const
bool debugStream (QDebug &dbg, const void *rhs)
 Streams the object at rhs to the debug stream dbg.
bool hasRegisteredDebugStreamOperator () const
constexpr const QtPrivate::QMetaTypeInterfaceiface () const

Static Public Member Functions

static void registerNormalizedTypedef (const QT_PREPEND_NAMESPACE(QByteArray) &normalizedTypeName, QMetaType type)
static bool isRegistered (int type)
 Returns true if the datatype with ID type is registered; otherwise returns false.
template<typename T>
static constexpr QMetaType fromType ()
static QMetaType fromName (QByteArrayView name)
 Returns a QMetaType matching typeName.
template<typename From, typename To>
static bool registerConverter ()
template<typename From, typename To>
static bool registerConverter (To(From::*function)() const)
template<typename From, typename To>
static bool registerMutableView (To(From::*function)())
template<typename From, typename To>
static bool registerConverter (To(From::*function)(bool *) const)
template<typename From, typename To, typename UnaryFunction>
static bool registerConverter (UnaryFunction function)
template<typename From, typename To, typename UnaryFunction>
static bool registerMutableView (UnaryFunction function)
static bool convert (QMetaType fromType, const void *from, QMetaType toType, void *to)
 Converts the object at from from fromType to the preallocated space at to typed toType.
static bool canConvert (QMetaType fromType, QMetaType toType)
 Returns true if QMetaType::convert can convert from fromType to toType.
static bool view (QMetaType fromType, void *from, QMetaType toType, void *to)
 Creates a mutable view on the object at from of fromType in the preallocated space at to typed toType.
static bool canView (QMetaType fromType, QMetaType toType)
 Returns true if QMetaType::view can create a mutable view of type toType on type fromType.
template<typename From, typename To>
static bool hasRegisteredConverterFunction ()
 Returns true, if the meta type system has a registered conversion from type From to type To.
static bool hasRegisteredConverterFunction (QMetaType fromType, QMetaType toType)
 Returns true, if the meta type system has a registered conversion from meta type id fromType to toType.
template<typename From, typename To>
static bool hasRegisteredMutableViewFunction ()
 Returns true, if the meta type system has a registered mutable view on type From of type To.
static bool hasRegisteredMutableViewFunction (QMetaType fromType, QMetaType toType)
 Returns true, if the meta type system has a registered mutable view on meta type id fromType of meta type id toType.
static bool registerConverterFunction (const ConverterFunction &f, QMetaType from, QMetaType to)
 Registers function f as converter function from type id from to to.
static void unregisterConverterFunction (QMetaType from, QMetaType to)
static bool registerMutableViewFunction (const MutableViewFunction &f, QMetaType from, QMetaType to)
 Registers function f as mutable view of type id to on type id from.
static void unregisterMutableViewFunction (QMetaType from, QMetaType to)
static void unregisterMetaType (QMetaType type)

Friends

template<typename, bool>
struct QtPrivate::SequentialValueTypeIsMetaType
template<typename, bool>
struct QtPrivate::AssociativeValueTypeIsMetaType
template<typename, bool>
struct QtPrivate::IsMetaTypePair
template<typename, typename>
struct QtPrivate::MetaTypeSmartPointerHelper
class QVariant
bool comparesEqual (const QMetaType &lhs, const QMetaType &rhs)
Q_CORE_EXPORT QDebug operator<< (QDebug d, QMetaType m)
int qRegisterMetaType (QMetaType meta)
 \threadsafe

(Note that these are not member symbols.)

template< typename T > int qRegisterMetaType (const char *typeName)
 \obsolete \threadsafe
template< typename T > int qRegisterMetaType ()
 \threadsafe
template< typename T > int qMetaTypeId ()
 \threadsafe

Detailed Description

\inmodule QtCore

The QMetaType class manages named types in the meta-object system.

\threadsafe \compares equality

The class is used as a helper to marshall types in QVariant and in queued signals and slots connections. It associates a type name to a type so that it can be created and destructed dynamically at run-time.

Type names can be registered with QMetaType by using either qRegisterMetaType() or registerType(). Registration is not required for most operations; it's only required for operations that attempt to resolve a type name in string form back to a QMetaType object or the type's ID. Those include some old-style signal-slot connections using QObject::connect(), reading user-types from \l QDataStream to \l QVariant, or binding to other languages and IPC mechanisms, like QML, D-Bus, JavaScript, etc.

The following code allocates and destructs an instance of {MyClass} by its name, which requires that {MyClass} have been previously registered:

if (type.isValid()) {
void *myClassPtr = type.create();
//...
type.destroy(myClassPtr);
myClassPtr = nullptr;
}

If we want the stream operators operator<<() and operator>>() to work on QVariant objects that store custom types, the custom type must provide operator<<() and operator>>() operators.

See also
Q_DECLARE_METATYPE(), QVariant::setValue(), QVariant::value(), QVariant::fromValue()

Definition at line 339 of file qmetatype.h.

Member Typedef Documentation

◆ ConverterFunction

using QMetaType::ConverterFunction = std::function<bool(const void *src, void *target)>

Definition at line 559 of file qmetatype.h.

◆ MutableViewFunction

using QMetaType::MutableViewFunction = std::function<bool(void *src, void *target)>

Definition at line 562 of file qmetatype.h.

Member Enumeration Documentation

◆ Type

\macro Q_DECLARE_OPAQUE_POINTER(PointerType)

Since
5.0

This macro enables pointers to forward-declared types (PointerType) to be registered with QMetaType using either Q_DECLARE_METATYPE() or qRegisterMetaType().

Do not use this macro to avoid complaints or errors from \l moc about incomplete property types when the pointee type is used as a complete type in other contexts in the program. Use \l{Q_MOC_INCLUDE} instead when the full definition of the type is available, but you prefer a forward declaration in the header to reduce compilation times.

Warning
Don't use Q_DECLARE_OPAQUE_POINTER with pointers to a Q_OBJECT or a gadget class, as that risks introducing inconsistent information in the meta-type system.
See also
Q_DECLARE_METATYPE(), qRegisterMetaType()

\macro Q_DECLARE_METATYPE(Type)

This macro makes the type Type known to 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.

This macro requires that 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.

This example shows a typical use case of Q_DECLARE_METATYPE():

struct MyStruct
{
int i;
//...
};

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

Since {MyStruct} is now known to QMetaType, it can be used in QVariant:

QVariant var;
var.setValue(s); // copy s into the variant
//...
// retrieve the value

Some types are registered automatically and do not need this macro:

\list

Note
This method also registers the stream and debug operators for the type if they are visible at registration time. As this is done automatically in some places, it is strongly recommended to declare the stream operators for a type directly after the type itself. Because of the argument dependent lookup rules of C++, it is also strongly recommended to declare the operators in the same namespace as the type itself.

The stream operators should have the following signatures:

See also
qRegisterMetaType()

\macro Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(Container)

This macro makes the container Container known to 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>
void someFunc()
{
std::deque<QFile*> container;
QVariant var = QVariant::fromValue(container);
// ...
}

\macro Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE(Container)

This macro makes the container Container known to 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_map>
{
std::unordered_map<int, bool> container;
QVariant var = QVariant::fromValue(container);
// ...
}

\macro Q_DECLARE_SMART_POINTER_METATYPE(SmartPointer)

This macro makes the smart pointer SmartPointer known to 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.

Note that the 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>
void someMethod()
{
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'.
}
}

These are the built-in types supported by QMetaType:

\value Void void \value Bool bool \value Int int \value UInt {unsigned int} \value Double double \value QChar QChar \value QString QString \value QByteArray QByteArray \value Nullptr {std::nullptr_t}

\value VoidStar {void *} \value Long {long} \value LongLong LongLong \value Short {short} \value Char {char} \value Char16 {char16_t} \value Char32 {char32_t} \value ULong {unsigned long} \value ULongLong ULongLong \value UShort {unsigned short} \value SChar {signed char} \value UChar {unsigned char} \value Float float \value Float16 qfloat16 \omitvalue Float128 \omitvalue BFloat16 \omitvalue Int128 \omitvalue UInt128 \value QObjectStar QObject *

\value QBitArray QBitArray \value QBitmap QBitmap \value QBrush QBrush \value QByteArrayList QByteArrayList \value QCborArray QCborArray \value QCborMap QCborMap \value QCborSimpleType QCborSimpleType \value QCborValue QCborValue \value QColor QColor \value QColorSpace QColorSpace (introduced in Qt 5.15) \value QCursor QCursor \value QDate QDate \value QDateTime QDateTime \value QEasingCurve QEasingCurve \value QFont QFont \value QIcon QIcon \value QImage QImage \value QJsonArray QJsonArray \value QJsonDocument QJsonDocument \value QJsonObject QJsonObject \value QJsonValue QJsonValue \value QKeySequence QKeySequence \value QLine QLine \value QLineF QLineF \value QLocale QLocale \value QMatrix4x4 QMatrix4x4 \value QModelIndex QModelIndex \value QPalette QPalette \value QPen QPen \value QPersistentModelIndex QPersistentModelIndex (introduced in Qt 5.5) \value QPixmap QPixmap \value QPoint QPoint \value QPointF QPointF \value QPolygon QPolygon \value QPolygonF QPolygonF \value QQuaternion QQuaternion \value QRect QRect \value QRectF QRectF \value QRegion QRegion \value QRegularExpression QRegularExpression \value QSize QSize \value QSizeF QSizeF \value QSizePolicy QSizePolicy \value QStringList QStringList \value QTextFormat QTextFormat \value QTextLength QTextLength \value QTime QTime \value QTransform QTransform \value QUrl QUrl \value QUuid QUuid \value QVariant QVariant \value QVariantHash QVariantHash \value QVariantList QVariantList \value QVariantMap QVariantMap \value QVariantPair QVariantPair \value QVector2D QVector2D \value QVector3D QVector3D \value QVector4D QVector4D

\value User Base value for user types \value UnknownType This is an invalid type id. It is returned from QMetaType for types that are not registered

Additional types can be registered using qRegisterMetaType() or by calling registerType().

See also
type()
Enumerator
FirstCoreType 
LastCoreType 
FirstGuiType 
LastGuiType 
FirstWidgetsType 
LastWidgetsType 
HighestInternalId 
QReal 
UnknownType 
User 

Definition at line 343 of file qmetatype.h.

◆ TypeFlag

The enum describes attributes of a type supported by QMetaType.

\value NeedsConstruction This type has a default constructor. If the flag is not set, instances can be safely initialized with memset to 0. \value [since 6.5] NeedsCopyConstruction This type has a non-trivial copy constructor. If the flag is not set, instances can be copied with memcpy. \value [since 6.5] NeedsMoveConstruction This type has a non-trivial move constructor. If the flag is not set, instances can be moved with memcpy. \value NeedsDestruction This type has a non-trivial destructor. If the flag is not set, calls to the destructor are not necessary before discarding objects. \value RelocatableType An instance of a type having this attribute can be safely moved to a different memory location using memcpy. \omitvalue MovableType \omitvalue SharedPointerToQObject \value IsEnumeration This type is an enumeration. \value IsUnsignedEnumeration If the type is an Enumeration, its underlying type is unsigned. \value PointerToQObject This type is a pointer to a class derived from QObject. \value IsPointer This type is a pointer to another type. \omitvalue WeakPointerToQObject \omitvalue TrackingPointerToQObject \omitvalue IsGadget \omit (since Qt 5.5) This type is a Q_GADGET and its corresponding QMetaObject can be accessed with QMetaType::metaObject. \endomit \omitvalue PointerToGadget \omitvalue IsQmlList \value IsConst Indicates that values of this type are immutable; for instance, because they are pointers to const objects.

Note
Before Qt 6.5, both the NeedsConstruction and NeedsDestruction flags were incorrectly set if the either copy construtor or destructor were non-trivial (that is, if the type was not trivial).

Note that the Needs flags may be set but the meta type may not have a publicly-accessible constructor of the relevant type or a publicly-accessible destructor.

Enumerator
NeedsConstruction 
NeedsDestruction 
RelocatableType 
PointerToQObject 
IsEnumeration 
SharedPointerToQObject 
WeakPointerToQObject 
TrackingPointerToQObject 
IsUnsignedEnumeration 
IsGadget 
PointerToGadget 
IsPointer 
IsQmlList 
IsConst 
NeedsCopyConstruction 
NeedsMoveConstruction 

Definition at line 397 of file qmetatype.h.

Constructor & Destructor Documentation

◆ QMetaType() [1/3]

QMetaType::QMetaType ( int typeId)
explicit
Since
5.0

Constructs a QMetaType object that contains all information about type typeId.

Definition at line 3365 of file qmetatype.cpp.

◆ QMetaType() [2/3]

QMetaType::QMetaType ( const QtPrivate::QMetaTypeInterface * d)
inlineexplicitconstexpr

Definition at line 458 of file qmetatype.h.

◆ QMetaType() [3/3]

QMetaType::QMetaType ( )
constexprdefault
Since
6.0

Constructs a default, invalid, QMetaType object.

Member Function Documentation

◆ alignOf()

qsizetype QMetaType::alignOf ( ) const
constexpr
Since
6.0

Returns the alignment of the type in bytes (i.e. alignof(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.

See also
QMetaType::construct(), QMetaType::sizeOf()

Definition at line 2704 of file qmetatype.h.

◆ canConvert()

bool QMetaType::canConvert ( QMetaType fromType,
QMetaType toType )
static

Returns true if QMetaType::convert can convert from fromType to toType.

Note this is mostly about the ability to execute the conversion, while the actual conversion may fail when attempted (for example, converting a floating point value to an integer outside of its range).

The registerConverter() function can be used to register additional conversions, either between a built-in type and a non-built-in one, or between two non-built-in types. This function will return true if the conversion path is registered.

The following conversions are supported by Qt:

\table \header

Other supported conversions include between all primitive types (int, float, bool, etc., including all enums) and between any pointer type and {std::nullptr_t}. Enumerations can also be converted to QString and QByteArray.

If both fromType and toType are types deriving from QObject (or pointers to them), this function will also return true if one of the types is derived from the other. That is, it returns true if {static_cast<>} from the type described by fromType to the type described by toType would compile. The convert() function operates like qobject_cast() and verifies the dynamic type of the object pointed to by the QVariant.

A cast from a sequential container will also return true for this function if the toType is QVariantList.

Similarly, a cast from an associative container will also return true for this function the toType is QVariantHash or QVariantMap.

See also
convert(), QSequentialIterable, Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(), QAssociativeIterable, Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE()

Definition at line 2664 of file qmetatype.cpp.

◆ canView()

bool QMetaType::canView ( QMetaType fromType,
QMetaType toType )
static

Returns true if QMetaType::view can create a mutable view of type toType on type fromType.

Converting between pointers of types derived from QObject will return true for this function if a qobject_cast from the type described by fromType to the type described by toType would succeed.

You can create a mutable view of type QSequentialIterable on any container registered with Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE().

Similarly you can create a mutable view of type QAssociativeIterable on any container registered with Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE().

See also
convert(), QSequentialIterable, Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(), QAssociativeIterable, Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE()

Definition at line 2555 of file qmetatype.cpp.

◆ compare()

QPartialOrdering QMetaType::compare ( const void * lhs,
const void * rhs ) const

Compares the objects at lhs and rhs for ordering.

Returns QPartialOrdering::Unordered if comparison is not supported or the values are unordered. Otherwise, returns QPartialOrdering::Less, QPartialOrdering::Equivalent or QPartialOrdering::Greater if lhs is less than, equivalent to or greater than rhs, respectively.

Both objects must be of the type described by this metatype. If either lhs or rhs is \nullptr, the values are unordered. Comparison is only supported if the type's less than operator was visible to the metatype declaration.

If the type's equality operator was also visible, values will only compare equal if the equality operator says they are. In the absence of an equality operator, when neither value is less than the other, values are considered equal; if equality is also available and two such values are not equal, they are considered unordered, just as NaN (not a number) values of a floating point type lie outside its ordering.

Note
If no less than operator was visible to the metatype declaration, values are unordered even if an equality operator visible to the declaration considers them equal: {compare() == 0} only agrees with equals() if the less than operator was visible.
Since
6.0
See also
equals(), isOrdered()
Deprecated
Use the non-static compare method instead

Compares the objects at 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. Returns true, if the comparison succeeded, otherwise false.

Deprecated
Use the non-static compare method instead

Compares the objects at 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. Returns true, if the comparison succeeded, otherwise false.

Definition at line 800 of file qmetatype.cpp.

◆ construct()

void * QMetaType::construct ( void * where,
const void * copy = nullptr ) const
Since
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, and returns where. If 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).

Since
5.0
Deprecated

Constructs a value of the given type in the existing memory addressed by where, that is a copy of copy, and returns where. If 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).

See also
destruct(), sizeOf()
Since
5.0
Deprecated

Constructs a value of the given type in the existing memory addressed by where, that is a copy of copy, and returns where. If 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).

See also
destruct(), sizeOf()

Definition at line 736 of file qmetatype.cpp.

◆ convert()

bool QMetaType::convert ( QMetaType fromType,
const void * from,
QMetaType toType,
void * to )
static

Converts the object at from from fromType to the preallocated space at to typed toType.

Deprecated

Converts the object at from from fromTypeId to the preallocated space at to typed toTypeId. Returns true, if the conversion succeeded, otherwise false.

Both from and to have to be valid pointers.

Since
5.2

Returns true, if the conversion succeeded, otherwise false.

Both from and to have to be valid pointers.

Since
5.2
Deprecated

Converts the object at from from fromTypeId to the preallocated space at to typed toTypeId. Returns true, if the conversion succeeded, otherwise false.

Both from and to have to be valid pointers.

Since
5.2

Definition at line 2457 of file qmetatype.cpp.

◆ create()

void * QMetaType::create ( const void * copy = nullptr) const
Since
5.0

Returns a copy of copy, assuming it is of the type that this QMetaType instance was created for. If copy is \nullptr, creates a default constructed instance.

See also
QMetaType::destroy()
Deprecated

Returns a copy of copy, assuming it is of type type. If copy is zero, creates a default constructed instance.

See also
destroy(), isRegistered(), Type
Deprecated

Returns a copy of copy, assuming it is of type type. If copy is zero, creates a default constructed instance.

See also
destroy(), isRegistered(), Type

Definition at line 678 of file qmetatype.cpp.

◆ debugStream()

bool QMetaType::debugStream ( QDebug & dbg,
const void * rhs )

Streams the object at rhs to the debug stream dbg.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns true on success, otherwise false.

Since
5.2
Deprecated
Deprecated

Definition at line 1953 of file qmetatype.cpp.

◆ destroy()

void QMetaType::destroy ( void * data) const
Since
5.0

Destroys the data, assuming it is of the type that this QMetaType instance was created for.

See also
QMetaType::create()
Deprecated
Destroys the data, assuming it is of the type given.
See also
create(), isRegistered(), Type
Deprecated
Destroys the data, assuming it is of the type given.
See also
create(), isRegistered(), Type

Definition at line 702 of file qmetatype.cpp.

◆ destruct()

void QMetaType::destruct ( void * data) const
Since
5.0

Destructs the value, located at data, assuming that it is of the type for which this QMetaType instance was constructed for.

Unlike destroy(), this function only invokes the type's destructor, it doesn't invoke the delete operator.

See also
QMetaType::construct()
Since
5.0
Deprecated

Destructs the value of the given type, located at where.

Unlike destroy(), this function only invokes the type's destructor, it doesn't invoke the delete operator.

See also
construct()
Since
5.0
Deprecated

Destructs the value of the given type, located at where.

Unlike destroy(), this function only invokes the type's destructor, it doesn't invoke the delete operator.

See also
construct()

Definition at line 758 of file qmetatype.cpp.

◆ equals()

bool QMetaType::equals ( const void * lhs,
const void * rhs ) const

Compares the objects at lhs and rhs for equality.

Both objects must be of the type described by this metatype. Can only compare the two objects if a less than or equality operator for the type was visible to the metatype declaration. Otherwise, the metatype never considers values equal. When an equality operator was visible to the metatype declaration, it is authoritative; otherwise, if less than is visible, when neither value is less than the other, the two are considered equal. If values are unordered (see compare() for details) they are not equal.

Returns true if the two objects compare equal, otherwise false.

Since
6.0
See also
isEqualityComparable(), compare()

Definition at line 836 of file qmetatype.cpp.

◆ flags()

QMetaType::TypeFlags QMetaType::flags ( ) const
constexpr
Since
5.0

Returns flags of the type for which this QMetaType instance was constructed. To inspect specific type traits, prefer using one of the "is-" functions rather than the flags directly.

See also
QMetaType::TypeFlags, isDefaultConstructible(), isCopyConstructible(), isMoveConstructible(), isDestructible(), isEqualityComparable(), isOrdered()

Definition at line 2709 of file qmetatype.h.

◆ fromName()

QMetaType QMetaType::fromName ( QByteArrayView typeName)
static

Returns a QMetaType matching typeName.

The returned object is not valid if the typeName is not known to QMetaType

Definition at line 3118 of file qmetatype.cpp.

◆ fromType()

template<typename T>
QMetaType QMetaType::fromType ( )
staticconstexpr
Since
5.15

Returns the QMetaType corresponding to the type in the template parameter.

Definition at line 2683 of file qmetatype.h.

◆ hasRegisteredConverterFunction() [1/2]

template<typename From, typename To>
bool QMetaType::hasRegisteredConverterFunction ( )
inlinestatic

Returns true, if the meta type system has a registered conversion from type From to type To.

Since
5.2 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 739 of file qmetatype.h.

◆ hasRegisteredConverterFunction() [2/2]

bool QMetaType::hasRegisteredConverterFunction ( QMetaType fromType,
QMetaType toType )
static

Returns true, if the meta type system has a registered conversion from meta type id fromType to toType.

Since
5.2

Definition at line 2741 of file qmetatype.cpp.

◆ hasRegisteredDataStreamOperators()

bool QMetaType::hasRegisteredDataStreamOperators ( ) const
Since
6.1

Returns true, if the meta type system has registered data stream operators for this meta type.

Definition at line 3048 of file qmetatype.cpp.

◆ hasRegisteredDebugStreamOperator()

bool QMetaType::hasRegisteredDebugStreamOperator ( ) const
Deprecated
Since
5.2

Returns true, if the meta type system has a registered debug stream operator for type T.

Deprecated
Use QMetaType::hasRegisteredDebugStreamOperator() instead.

Returns true, if the meta type system has a registered debug stream operator for type id typeId.

Since
5.2
6.0

Returns true, if the meta type system has a registered debug stream operator for this meta type.

Deprecated
Since
5.2

Returns true, if the meta type system has a registered debug stream operator for type T.

Deprecated
Use QMetaType::hasRegisteredDebugStreamOperator() instead.

Returns true, if the meta type system has a registered debug stream operator for type id typeId.

Since
5.2

Definition at line 1995 of file qmetatype.cpp.

◆ hasRegisteredMutableViewFunction() [1/2]

template<typename From, typename To>
bool QMetaType::hasRegisteredMutableViewFunction ( )
inlinestatic

Returns true, if the meta type system has a registered mutable view on type From of type To.

Since
6.0 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 748 of file qmetatype.h.

◆ hasRegisteredMutableViewFunction() [2/2]

bool QMetaType::hasRegisteredMutableViewFunction ( QMetaType fromType,
QMetaType toType )
static

Returns true, if the meta type system has a registered mutable view on meta type id fromType of meta type id toType.

Since
5.2

Definition at line 2788 of file qmetatype.cpp.

◆ id()

int QMetaType::id ( int = 0) const
inline
Since
5.13

Returns id type held by this QMetatype instance.

Definition at line 477 of file qmetatype.h.

◆ iface()

const QtPrivate::QMetaTypeInterface * QMetaType::iface ( ) const
inlineconstexpr

Definition at line 773 of file qmetatype.h.

◆ isCopyConstructible()

bool QMetaType::isCopyConstructible ( ) const
inlinenoexcept
Since
6.5

Returns true if this type can be copy-constructed. If it can be, then construct() and create() can be used with a {copy} parameter that is not null.

See also
flags(), isDefaultConstructible(), isMoveConstructible(), isDestructible()

Definition at line 496 of file qmetatype.h.

◆ isDefaultConstructible()

bool QMetaType::isDefaultConstructible ( ) const
inlinenoexcept
Since
6.5

Returns true if this type can be default-constructed. If it can be, then construct() and create() can be used with a {copy} parameter that is null.

See also
flags(), isCopyConstructible(), isMoveConstructible(), isDestructible()

Definition at line 495 of file qmetatype.h.

◆ isDestructible()

bool QMetaType::isDestructible ( ) const
inlinenoexcept
Since
6.5

Returns true if this type can be destroyed. If it can be, then destroy() and destruct() can be called.

See also
flags(), isDefaultConstructible(), isCopyConstructible(), isMoveConstructible()

Definition at line 498 of file qmetatype.h.

◆ isEqualityComparable()

bool QMetaType::isEqualityComparable ( ) const

Returns true if a less than or equality operator for the type described by this metatype was visible to the metatype declaration, otherwise false.

See also
equals(), isOrdered()

Definition at line 920 of file qmetatype.cpp.

◆ isMoveConstructible()

bool QMetaType::isMoveConstructible ( ) const
inlinenoexcept
Since
6.5

Returns true if this type can be move-constructed. QMetaType currently does not have an API to make use of this trait.

See also
flags(), isDefaultConstructible(), isCopyConstructible(), isDestructible()

Definition at line 497 of file qmetatype.h.

◆ isOrdered()

bool QMetaType::isOrdered ( ) const

Returns true if a less than operator for the type described by this metatype was visible to the metatype declaration, otherwise false.

See also
compare(), isEqualityComparable()

Definition at line 931 of file qmetatype.cpp.

◆ isRegistered() [1/2]

bool QMetaType::isRegistered ( int type)
static

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

See also
type(), Type

Definition at line 2906 of file qmetatype.cpp.

◆ isRegistered() [2/2]

bool QMetaType::isRegistered ( QT6_DECL_NEW_OVERLOAD ) const
inlinenoexcept

◆ isValid()

bool QMetaType::isValid ( QT6_DECL_NEW_OVERLOAD ) const
constexprnoexcept
Since
5.0

Returns true if this QMetaType object contains valid information about a type, false otherwise.

See also
isRegistered()

Definition at line 2689 of file qmetatype.h.

◆ load()

bool QMetaType::load ( QDataStream & stream,
void * data ) const

Reads the object of this type from the given stream into data.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns true if the object is loaded successfully; otherwise returns false.

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

See also
save()
Deprecated
Deprecated

Definition at line 3018 of file qmetatype.cpp.

◆ metaObject()

const QMetaObject * QMetaType::metaObject ( ) const
constexpr
Since
5.5

Returns 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 in combination with QMetaObject::newInstance() to create QObjects of this type.

If the type is a Q_GADGET, flags() contains QMetaType::IsGadget. If the type is a pointer to a Q_GADGET, flags() contains QMetaType::PointerToGadget. In both cases, this function returns its QMetaObject. This can be used to retrieve QMetaMethod and QMetaProperty and use them on a pointer of this type for example, as given by QVariant::data().

If the type is an enumeration, flags() contains QMetaType::IsEnumeration. In this case, this function returns the QMetaObject of the enclosing object if the enum was registered as a Q_ENUM or \nullptr otherwise.

See also
QMetaType::flags()

Definition at line 2714 of file qmetatype.h.

◆ name()

const char * QMetaType::name ( ) const
constexpr
Since
5.15

Returns the type name associated with this QMetaType, or a null pointer if no matching type was found. The returned pointer must not be deleted.

See also
typeName()

Definition at line 2719 of file qmetatype.h.

◆ registerConverter() [1/4]

template<typename From, typename To>
bool QMetaType::registerConverter ( )
inlinestatic
Since
5.2 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.
class Counter {
int number = 0;
public:
int value() const { return number; }
operator int() const { return value(); }
void increment() {++number;}
};

Definition at line 566 of file qmetatype.h.

◆ registerConverter() [2/4]

template<typename From, typename To>
bool QMetaType::registerConverter ( To(From::* function )() const)
inlinestatic
Since
5.2 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. 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.
struct Coordinates {
int x;
int y;
int z;
QString toString() const { return u"[x: %1; y: %2, z: %3]"_s.arg(QString::number(x),
};

Definition at line 573 of file qmetatype.h.

◆ registerConverter() [3/4]

template<typename From, typename To>
bool QMetaType::registerConverter ( To(From::* function )(bool *) const)
inlinestatic
Since
5.2 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. 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.

The ok pointer can be used by the function to indicate whether the conversion succeeded.

struct BigNumber {
long long l;
int toInt(bool *ok = nullptr) const {
const bool canConvertSafely = l < std::numeric_limits<int>::max();
if (ok)
*ok = canConvertSafely;
return l;
}
};

Definition at line 609 of file qmetatype.h.

◆ registerConverter() [4/4]

template<typename From, typename To, typename UnaryFunction>
bool QMetaType::registerConverter ( UnaryFunction function)
inlinestatic
Since
5.2 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. 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.

function must take an instance of type From and return an instance of To. It can be a function pointer, a lambda or a functor object. Since Qt 6.5, the function can also return an instance of std::optional<To> to be able to indicate failed conversions.

return QString::fromUtf8(str.data());
});
[](const QJsonValue &value) -> std::optional<CustomPointType> {
const auto object = value.toObject();
if (!object.contains("x") || !object.contains("y"))
return std::nullopt; // The conversion fails if the required properties are missing
return CustomPointType{object["x"].toDouble(), object["y"].toDouble()};
});

Definition at line 630 of file qmetatype.h.

◆ registerConverterFunction()

bool QMetaType::registerConverterFunction ( const ConverterFunction & f,
QMetaType from,
QMetaType to )
static

Registers function f as converter function from type id from to to.

If there's already a conversion registered, this does nothing but deleting f. Returns true if the registration succeeded, otherwise false.

Since
5.2

Definition at line 1869 of file qmetatype.cpp.

◆ registerMutableView() [1/2]

template<typename From, typename To>
bool QMetaType::registerMutableView ( To(From::* function )())
inlinestatic
Since
6.0 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Registers a method function like {To From::function()} as mutable view of type {To} on type {From} in the meta type system. Returns true if the registration succeeded, otherwise false.

Definition at line 591 of file qmetatype.h.

◆ registerMutableView() [2/2]

template<typename From, typename To, typename UnaryFunction>
bool QMetaType::registerMutableView ( UnaryFunction function)
inlinestatic
Since
6.0 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Registers a unary function object function as mutable view of type To on type From in the meta type system. Returns true if the registration succeeded, otherwise false.

Definition at line 655 of file qmetatype.h.

◆ registerMutableViewFunction()

bool QMetaType::registerMutableViewFunction ( const MutableViewFunction & f,
QMetaType from,
QMetaType to )
static

Registers function f as mutable view of type id to on type id from.

Returns true if the registration succeeded, otherwise false.

Since
6.0

Definition at line 1902 of file qmetatype.cpp.

◆ registerNormalizedTypedef()

void QMetaType::registerNormalizedTypedef ( const QT_PREPEND_NAMESPACE(QByteArray) & normalizedTypeName,
QMetaType type )
static

Registers a user type for marshalling, as an alias of another type (typedef). Note that normalizedTypeName is not checked for conformance with Qt's normalized format, so it must already conform.

Definition at line 2856 of file qmetatype.cpp.

◆ registerType()

void QMetaType::registerType ( ) const
inline
Since
6.5

Registers this QMetaType with the type registry so it can be found by name, using QMetaType::fromName().

See also
qRegisterMetaType()

Definition at line 467 of file qmetatype.h.

◆ save()

bool QMetaType::save ( QDataStream & stream,
const void * data ) const

Writes the object pointed to by data to the given stream.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns true if the object is saved successfully; otherwise returns false.

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

See also
load()
Deprecated
Deprecated

Definition at line 2980 of file qmetatype.cpp.

◆ sizeOf()

qsizetype QMetaType::sizeOf ( ) const
constexpr
Since
5.0

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.

See also
QMetaType::construct(), QMetaType::alignOf()
Since
5.0
Deprecated

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.

See also
construct(), QMetaType::alignOf()
Since
5.0
Deprecated

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.

See also
construct(), QMetaType::alignOf()

Definition at line 2699 of file qmetatype.h.

◆ underlyingType()

QMetaType QMetaType::underlyingType ( ) const
Since
6.6

If this metatype represents an enumeration, this method returns a metatype of a numeric class of the same signedness and size as the enums underlying type. If it represents a QFlags type, it returns QMetaType::Int. In all other cases an invalid QMetaType is returned.

Definition at line 3065 of file qmetatype.cpp.

◆ unregisterConverterFunction()

void QMetaType::unregisterConverterFunction ( QMetaType from,
QMetaType to )
static

Invoked automatically when a converter function object is destroyed.

Definition at line 1927 of file qmetatype.cpp.

◆ unregisterMetaType()

void QMetaType::unregisterMetaType ( QMetaType type)
static

Definition at line 940 of file qmetatype.cpp.

◆ unregisterMutableViewFunction()

void QMetaType::unregisterMutableViewFunction ( QMetaType from,
QMetaType to )
static

Definition at line 1915 of file qmetatype.cpp.

◆ view()

bool QMetaType::view ( QMetaType fromType,
void * from,
QMetaType toType,
void * to )
static

Creates a mutable view on the object at from of fromType in the preallocated space at to typed toType.

Returns true if the conversion succeeded, otherwise false.

Since
6.0

Definition at line 2517 of file qmetatype.cpp.

◆ comparesEqual

bool comparesEqual ( const QMetaType & lhs,
const QMetaType & rhs )
friend

Definition at line 523 of file qmetatype.h.

◆ operator<<

Q_CORE_EXPORT QDebug operator<< ( QDebug d,
QMetaType m )
friend
Since
6.5 Writes the QMetaType m to the stream d, and returns the stream.

Definition at line 1942 of file qmetatype.cpp.

◆ qMetaTypeId()

template< typename T > int qMetaTypeId ( )
related

\threadsafe

Since
4.1

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

Typical usage:

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.

See also
Q_DECLARE_METATYPE(), QMetaType::type()

Definition at line 1387 of file qmetatype.h.

◆ qRegisterMetaType() [1/3]

template< typename T > int qRegisterMetaType ( )
related

\threadsafe

Since
4.2

Call this function to register the type T. Returns the meta type Id.

Example:

This function requires that {T} is a fully defined type at the point where the function is called. For pointer types, it also requires that the pointed to type is fully defined. Use Q_DECLARE_OPAQUE_POINTER() to be able to register pointers to forward declared types.

To use the type T in QMetaType, QVariant, or with the QObject::property() API, registration is not necessary.

To use the type T in queued signal and slot connections, {qRegisterMetaType<T>()} must be called before the first connection is established. That is typically done in the constructor of the class that uses T, or in the {main()} function.

After a type has been registered, it can be found by its name using QMetaType::fromName().

See also
Q_DECLARE_METATYPE()

Definition at line 1400 of file qmetatype.h.

◆ qRegisterMetaType() [2/3]

template< typename T > int qRegisterMetaType ( const char * typeName)
related

\obsolete \threadsafe

Registers the type name typeName for the type {T}. Returns the internal ID used by QMetaType. Any class or struct that has a public default constructor, a public copy constructor and a public destructor can be registered.

This function requires that {T} is a fully defined type at the point where the function is called. For pointer types, it also requires that the pointed to type is fully defined. Use Q_DECLARE_OPAQUE_POINTER() to be able to register pointers to forward declared types.

After a type has been registered, you can create and destroy objects of that type dynamically at run-time.

This example registers the class {MyClass}:

This function is useful to register typedefs so they can be used by QMetaProperty, or in QueuedConnections

typedef QString CustomString;
Warning
This function is useful only for registering an alias (typedef) for every other use case Q_DECLARE_METATYPE and qMetaTypeId() should be used instead.
See also
{QMetaType::}{isRegistered()}, Q_DECLARE_METATYPE()

Definition at line 1376 of file qmetatype.h.

◆ qRegisterMetaType [3/3]

int qRegisterMetaType ( QMetaType meta)
friend

\threadsafe

Since
6.5

Registers the meta type meta and returns its type Id.

This function requires that {T} is a fully defined type at the point where the function is called. For pointer types, it also requires that the pointed to type is fully defined. Use Q_DECLARE_OPAQUE_POINTER() to be able to register pointers to forward declared types.

To use the type T in QMetaType, QVariant, or with the QObject::property() API, registration is not necessary.

To use the type T in queued signal and slot connections, {qRegisterMetaType<T>()} must be called before the first connection is established. That is typically done in the constructor of the class that uses T, or in the {main()} function.

After a type has been registered, it can be found by its name using QMetaType::fromName().

Definition at line 1406 of file qmetatype.h.

◆ QtPrivate::AssociativeValueTypeIsMetaType

template<typename, bool>
friend struct QtPrivate::AssociativeValueTypeIsMetaType
friend

Definition at line 758 of file qmetatype.h.

◆ QtPrivate::IsMetaTypePair

template<typename, bool>
friend struct QtPrivate::IsMetaTypePair
friend

Definition at line 759 of file qmetatype.h.

◆ QtPrivate::MetaTypeSmartPointerHelper

template<typename, typename>
friend struct QtPrivate::MetaTypeSmartPointerHelper
friend

Definition at line 760 of file qmetatype.h.

◆ QtPrivate::SequentialValueTypeIsMetaType

template<typename, bool>
friend struct QtPrivate::SequentialValueTypeIsMetaType
friend

Definition at line 757 of file qmetatype.h.

◆ QVariant

friend class QVariant
friend

Definition at line 797 of file qmetatype.h.


The documentation for this class was generated from the following files: