PySide6.QtCore.QMetaType¶
- class QMetaType¶
The
QMetaTypeclass manages named types in the meta-object system.Details
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
The class is used as a helper to marshall types in
QVariantand 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
QMetaTypeby using eitherqRegisterMetaType()orregisterType(). 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 aQMetaTypeobject or the type’s ID. Those include some old-style signal-slot connections usingconnect(), reading user-types fromQDataStreamtoQVariant, or binding to other languages and IPC mechanisms, like QML, D-Bus, JavaScript, etc.The following code allocates and destructs an instance of
MyClassby its name, which requires thatMyClasshave been previously registered:type = QMetaType.fromName("MyClass") if type.isValid(): myClassPtr = type.create() ... type.destroy(myClassPtr) myClassPtr = None
If we want the stream operators
operator<<()andoperator>>()to work onQVariantobjects that store custom types, the custom type must provideoperator<<()andoperator>>()operators.See also
Q_DECLARE_METATYPE()setValue()value()fromValue()Synopsis¶
Methods¶
def
__init__()def
alignOf()def
construct()def
create()def
destroy()def
destruct()def
equals()def
flags()def
id()def
isDestructible()def
isOrdered()def
isRegistered()def
isValid()def
load()def
name()def
__ne__()def
__eq__()def
registerType()def
save()def
sizeOf()def
underlyingType()
Static functions¶
def
canConvert()def
canView()def
compare()def
construct()def
convert()def
create()def
destroy()def
destruct()def
equals()def
fromName()def
isRegistered()def
load()def
save()def
sizeOf()def
type()def
typeFlags()def
typeName()def
view()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
- class Type¶
(inherits
enum.IntEnum) These are the built-in types supported byQMetaType:Constant
Description
QMetaType.Type.Void
voidQMetaType.Type.Bool
boolQMetaType.Type.Int
intQMetaType.Type.UInt
unsigned intQMetaType.Type.Double
doubleQMetaType.Type.QChar
QChar
QMetaType.Type.QString
QString
QMetaType.Type.QByteArray
QByteArray
QMetaType.Type.Nullptr
std::nullptr_tQMetaType.Type.VoidStar
void *QMetaType.Type.Long
longQMetaType.Type.LongLong
LongLong
QMetaType.Type.Short
shortQMetaType.Type.Char
charQMetaType.Type.Char16
char16_tQMetaType.Type.Char32
char32_tQMetaType.Type.ULong
unsigned longQMetaType.Type.ULongLong
ULongLong
QMetaType.Type.UShort
unsigned shortQMetaType.Type.SChar
signed charQMetaType.Type.UChar
unsigned charQMetaType.Type.Float
floatQMetaType.Type.Float16
qfloat16
QMetaType.Type.QObjectStar
QObject*QMetaType.Type.QBitArray
QBitArray
QMetaType.Type.QBitmap
QBitmap
QMetaType.Type.QBrush
QBrush
QMetaType.Type.QByteArrayList
QByteArrayList
QMetaType.Type.QCborArray
QCborArray
QMetaType.Type.QCborMap
QCborMap
QMetaType.Type.QCborSimpleType
QCborSimpleType
QMetaType.Type.QCborValue
QCborValue
QMetaType.Type.QColor
QColor
QMetaType.Type.QColorSpace
QColorSpace (introduced in Qt 5.15)
QMetaType.Type.QCursor
QCursor
QMetaType.Type.QDate
QDate
QMetaType.Type.QDateTime
QDateTime
QMetaType.Type.QEasingCurve
QEasingCurve
QMetaType.Type.QFont
QFont
QMetaType.Type.QIcon
QIcon
QMetaType.Type.QImage
QImage
QMetaType.Type.QJsonArray
QJsonArray
QMetaType.Type.QJsonDocument
QJsonDocument
QMetaType.Type.QJsonObject
QJsonObject
QMetaType.Type.QJsonValue
QJsonValue
QMetaType.Type.QKeySequence
QKeySequence
QMetaType.Type.QLine
QLine
QMetaType.Type.QLineF
QLineF
QMetaType.Type.QLocale
QLocale
QMetaType.Type.QMatrix4x4
QMatrix4x4
QMetaType.Type.QModelIndex
QModelIndex
QMetaType.Type.QPalette
QPalette
QMetaType.Type.QPen
QPen
QMetaType.Type.QPersistentModelIndex
QPersistentModelIndex (introduced in Qt 5.5)
QMetaType.Type.QPixmap
QPixmap
QMetaType.Type.QPoint
QPoint
QMetaType.Type.QPointF
QPointF
QMetaType.Type.QPolygon
QPolygon
QMetaType.Type.QPolygonF
QPolygonF
QMetaType.Type.QQuaternion
QQuaternion
QMetaType.Type.QRect
QRect
QMetaType.Type.QRectF
QRectF
QMetaType.Type.QRegion
QRegion
QMetaType.Type.QRegularExpression
QRegularExpression
QMetaType.Type.QSize
QSize
QMetaType.Type.QSizeF
QSizeF
QMetaType.Type.QSizePolicy
QSizePolicy
QMetaType.Type.QStringList
QStringList
QMetaType.Type.QTextFormat
QTextFormat
QMetaType.Type.QTextLength
QTextLength
QMetaType.Type.QTime
QTime
QMetaType.Type.QTransform
QTransform
QMetaType.Type.QUrl
QUrl
QMetaType.Type.QUuid
QUuid
QMetaType.Type.QVariant
QVariant
QMetaType.Type.QVariantHash
QVariantHash
QMetaType.Type.QVariantList
QVariantList
QMetaType.Type.QVariantMap
QVariantMap
QMetaType.Type.QVariantPair
QVariantPair
QMetaType.Type.QVector2D
QVector2D
QMetaType.Type.QVector3D
QVector3D
QMetaType.Type.QVector4D
QVector4D
QMetaType.Type.User
Base value for user types
QMetaType.Type.UnknownType
This is an invalid type id. It is returned from
QMetaTypefor types that are not registeredAdditional types can be registered using
qRegisterMetaType()or by callingregisterType().See also
- class TypeFlag¶
(inherits
enum.Flag) The enum describes attributes of a type supported byQMetaType.Constant
Description
QMetaType.TypeFlag.NeedsConstruction
This type has a default constructor. If the flag is not set, instances can be safely initialized with memset to 0.
QMetaType.TypeFlag.NeedsCopyConstruction
This type has a non-trivial copy constructor. If the flag is not set, instances can be copied with memcpy.
QMetaType.TypeFlag.NeedsMoveConstruction
This type has a non-trivial move constructor. If the flag is not set, instances can be moved with memcpy.
QMetaType.TypeFlag.NeedsDestruction
This type has a non-trivial destructor. If the flag is not set, calls to the destructor are not necessary before discarding objects.
QMetaType.TypeFlag.RelocatableType
An instance of a type having this attribute can be safely moved to a different memory location using memcpy.
QMetaType.TypeFlag.IsEnumeration
This type is an enumeration.
QMetaType.TypeFlag.IsUnsignedEnumeration
If the type is an Enumeration, its underlying type is unsigned.
QMetaType.TypeFlag.PointerToQObject
This type is a pointer to a class derived from
QObject.QMetaType.TypeFlag.IsPointer
This type is a pointer to another type.
QMetaType.TypeFlag.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.
- __init__()¶
Constructs a default, invalid,
QMetaTypeobject.- __init__(py_type)
- Parameters:
py_type –
PyTypeObject
- __init__(type)
- Parameters:
type –
Type
- __init__(type)
- Parameters:
type – int
Constructs a
QMetaTypeobject that contains all information about typetypeId.- alignOf()¶
- Return type:
int
Returns the alignment of the type in bytes (i.e. alignof(T), where T is the actual type for which this
QMetaTypeinstance 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
- static canConvert(fromType, toType)¶
Returns
trueifconvertcan convert fromfromTypetotoType. 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 returntrueif the conversion path is registered.The following conversions are supported by Qt:
Type
Automatically Cast To
QChar,Double,Int,LongLong,QString,UInt,ULongLongDouble,Int,LongLong,QString,UInt,ULongLong,QUuidBool,QChar,Double,LongLong,QString,UInt,ULongLongQStringList(if the list’s items can be converted to QStrings)Bool,QByteArray,QChar,Double,Int,QString,UInt,ULongLongBool,QByteArray,QChar,QColor,QDate,QDateTime,Double,QFont,Int,QKeySequence,LongLong,QStringList,QTime,UInt,ULongLong,QUuidQVariantList,QString(if the list contains exactly one item)Bool,QChar,Double,Int,LongLong,QString,ULongLongOther supported conversions include between all primitive types (
int,float,bool, etc., including all enums) and between any pointer type andstd::nullptr_t. Enumerations can also be converted toQStringandQByteArray.If both
fromTypeandtoTypeare types deriving fromQObject(or pointers to them), this function will also returntrueif one of the types is derived from the other. That is, it returns true ifstatic_cast<>from the type described byfromTypeto the type described bytoTypewould compile. Theconvert()function operates likeqobject_cast()and verifies the dynamic type of the object pointed to by theQVariant.A cast from a sequential container will also return true for this function if the
toTypeisQVariantList.Similarly, a cast from an associative container will also return true for this function the
toTypeisQVariantHashorQVariantMap.See also
convert()QSequentialIterableQ_DECLARE_SEQUENTIAL_CONTAINER_METATYPE()QAssociativeIterableQ_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE()- static canView(fromType, toType)¶
Returns
trueifviewcan create a mutable view of typetoTypeon typefromType.Converting between pointers of types derived from
QObjectwill return true for this function if aqobject_castfrom the type described byfromTypeto the type described bytoTypewould succeed.You can create a mutable view of type
QSequentialIterableon any container registered withQ_DECLARE_SEQUENTIAL_CONTAINER_METATYPE().Similarly you can create a mutable view of type
QAssociativeIterableon any container registered withQ_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE().See also
convert()QSequentialIterableQ_DECLARE_SEQUENTIAL_CONTAINER_METATYPE()QAssociativeIterableQ_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE()- static compare(lhs, rhs, typeId, result)¶
- Parameters:
lhs –
voidrhs –
voidtypeId – int
result – int
- Return type:
bool
Note
This function is deprecated.
Use the non-static compare method instead
Compares the objects at
lhsandrhs. Both objects need to be of typetypeId.resultis set to less than, equal to or greater than zero, iflhsis less than, equal to or greater thanrhs. Returnstrue, if the comparison succeeded, otherwisefalse.- construct(where[, copy=None])¶
- Parameters:
where –
voidcopy –
void
- Return type:
void
Constructs a value of the type that this
QMetaTypeinstance was constructed for in the existing memory addressed bywhere, that is a copy ofcopy, and returnswhere. Ifcopyis 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
wherepoints to a location where the new value can be stored and thatwhereis suitably aligned. The type’s size can be queried by callingsizeOf().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).
- static construct(type, where, copy)
- Parameters:
type – int
where –
voidcopy –
void
- Return type:
void
Note
This function is deprecated.
Constructs a value of the given
typein the existing memory addressed bywhere, that is a copy ofcopy, and returnswhere. Ifcopyis 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
wherepoints to a location that can store a value of typetype, and thatwhereis suitably aligned. The type’s size can be queried by callingsizeOf().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
- static convert(fromType, from, toType, to)¶
Converts the object at
fromfromfromTypeto the preallocated space attotypedtoType. Returnstrue, if the conversion succeeded, otherwise false.Both
fromandtohave to be valid pointers.- static convert(from, fromTypeId, to, toTypeId)
- Parameters:
from –
voidfromTypeId – int
to –
voidtoTypeId – int
- Return type:
bool
Note
This function is deprecated.
Converts the object at
fromfromfromTypeIdto the preallocated space attotypedtoTypeId. Returnstrue, if the conversion succeeded, otherwise false.Both
fromandtohave to be valid pointers.- create([copy=None])¶
- Parameters:
copy –
void- Return type:
void
Returns a copy of
copy, assuming it is of the type that thisQMetaTypeinstance was created for. IfcopyisNone, creates a default constructed instance.See also
- static create(type[, copy=None])
- Parameters:
type – int
copy –
void
- Return type:
void
Note
This function is deprecated.
Returns a copy of
copy, assuming it is of typetype. Ifcopyis zero, creates a default constructed instance.See also
- destroy(data)¶
- Parameters:
data –
void
Destroys the
data, assuming it is of the type that thisQMetaTypeinstance was created for.See also
- static destroy(type, data)
- Parameters:
type – int
data –
void
Note
This function is deprecated.
Destroys the
data, assuming it is of thetypegiven.See also
- destruct(data)¶
- Parameters:
data –
void
Destructs the value, located at
data, assuming that it is of the type for which thisQMetaTypeinstance was constructed for.Unlike
destroy(), this function only invokes the type’s destructor, it doesn’t invoke the delete operator.See also
- static destruct(type, where)
- Parameters:
type – int
where –
void
Note
This function is deprecated.
Destructs the value of the given
type, located atwhere.Unlike
destroy(), this function only invokes the type’s destructor, it doesn’t invoke the delete operator.See also
- equals(lhs, rhs)¶
- Parameters:
lhs –
voidrhs –
void
- Return type:
bool
Compares the objects at
lhsandrhsfor 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.
See also
- static equals(lhs, rhs, typeId, result)
- Parameters:
lhs –
voidrhs –
voidtypeId – int
result – int
- Return type:
bool
Note
This function is deprecated.
Returns flags of the type for which this
QMetaTypeinstance was constructed. To inspect specific type traits, prefer using one of the “is-” functions rather than the flags directly.Returns a
QMetaTypematchingtypeName. The returned object is not valid if the typeName is not known toQMetaType- static hasRegisteredConverterFunction(fromType, toType)¶
Returns
true, if the meta type system has a registered conversion from meta type idfromTypetotoType- hasRegisteredDataStreamOperators()¶
- Return type:
bool
Returns
true, if the meta type system has registered data stream operators for this meta type.- hasRegisteredDebugStreamOperator()¶
- Return type:
bool
Returns
true, if the meta type system has a registered debug stream operator for this meta type.- static hasRegisteredDebugStreamOperator(typeId)
- Parameters:
typeId – int
- Return type:
bool
Note
This function is deprecated.
Use
hasRegisteredDebugStreamOperator()instead.Returns
true, if the meta type system has a registered debug stream operator for type idtypeId.- static hasRegisteredMutableViewFunction(fromType, toType)¶
Returns
true, if the meta type system has a registered mutable view on meta type idfromTypeof meta type idtoType.- id([arg__1=0])¶
- Parameters:
arg__1 – int
- Return type:
int
- isCopyConstructible()¶
- Return type:
bool
Returns true if this type can be copy-constructed. If it can be, then
construct()andcreate()can be used with acopyparameter that is not null.- isDefaultConstructible()¶
- Return type:
bool
Returns true if this type can be default-constructed. If it can be, then
construct()andcreate()can be used with acopyparameter that is null.- isDestructible()¶
- Return type:
bool
Returns true if this type can be destroyed. If it can be, then
destroy()anddestruct()can be called.- isEqualityComparable()¶
- Return type:
bool
Returns
trueif a less than or equality operator for the type described by this metatype was visible to the metatype declaration, otherwisefalse.See also
- isMoveConstructible()¶
- Return type:
bool
Returns true if this type can be move-constructed.
QMetaTypecurrently does not have an API to make use of this trait.- isOrdered()¶
- Return type:
bool
Returns
trueif a less than operator for the type described by this metatype was visible to the metatype declaration, otherwisefalse.See also
- isRegistered()¶
- Return type:
bool
Returns
trueif thisQMetaTypeobject has been registered with the Qt global metatype registry. Registration allows the type to be found by its name (usingfromName()) or by its ID (using the constructor).See also
qRegisterMetaType()isValid()- static isRegistered(type)
- Parameters:
type – int
- Return type:
bool
Returns
trueif the datatype with IDtypeis registered; otherwise returnsfalse.- isValid()¶
- Return type:
bool
Returns
trueif thisQMetaTypeobject contains valid information about a type, false otherwise.See also
- load(stream, data)¶
- Parameters:
stream –
QDataStreamdata –
void
- Return type:
bool
Reads the object of this type from the given
streamintodata. Returnstrueif the object is loaded successfully; otherwise returnsfalse.Normally, you should not need to call this function directly. Instead, use
QVariant‘soperator>>(), which relies on load() to stream custom types.See also
- static load(stream, type, data)
- Parameters:
stream –
QDataStreamtype – int
data –
void
- Return type:
bool
Note
This function is deprecated.
- static metaObjectForType(type)¶
- Parameters:
type – int
- Return type:
Note
This function is deprecated.
returns
metaObjectfortypeSee also
metaObject()- name()¶
- Return type:
str
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
Returns
trueif theQMetaTypelhsrepresents a different type than theQMetaTyperhs, otherwise returnsfalse.Returns
trueif theQMetaTypelhsrepresents the same type as theQMetaTyperhs, otherwise returnsfalse.- static registerNormalizedTypedef(normalizedTypeName, type)¶
- Parameters:
normalizedTypeName –
QByteArraytype –
QMetaType
- registerType()¶
Registers this
QMetaTypewith the type registry so it can be found by name, usingfromName().See also
qRegisterMetaType()- save(stream, data)¶
- Parameters:
stream –
QDataStreamdata –
void
- Return type:
bool
Writes the object pointed to by
datato the givenstream. Returnstrueif the object is saved successfully; otherwise returnsfalse.Normally, you should not need to call this function directly. Instead, use
QVariant‘soperator<<(), which relies on save() to stream custom types.See also
- static save(stream, type, data)
- Parameters:
stream –
QDataStreamtype – int
data –
void
- Return type:
bool
Note
This function is deprecated.
- sizeOf()¶
- Return type:
int
Returns the size of the type in bytes (i.e. sizeof(T), where T is the actual type for which this
QMetaTypeinstance 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
- static sizeOf(type)
- Parameters:
type – int
- Return type:
int
Note
This function is deprecated.
Returns the size of the given
typein bytes (i.e. sizeof(T), where T is the actual type identified by thetypeargument).This function is typically used together with
construct()to perform low-level management of the memory used by a type.See also
- static type(typeName)¶
- Parameters:
typeName –
QByteArray- Return type:
int
Note
This function is deprecated.
Returns a handle to the type called
typeName, or 0 if there is no such type.See also
- static type(typeName)
- Parameters:
typeName – str
- Return type:
int
Note
This function is deprecated.
Returns a handle to the type called
typeName, orUnknownTypeif there is no such type.See also
- static typeFlags(type)¶
- Parameters:
type – int
- Return type:
Combination of
TypeFlag
Note
This function is deprecated.
Returns flags of the given
type.See also
TypeFlags- static typeName(type)¶
- Parameters:
type – int
- Return type:
str
Note
This function is deprecated.
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.See also
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
QFlagstype, it returnsInt. In all other cases an invalidQMetaTypeis returned.- static view(fromType, from, toType, to)¶
Creates a mutable view on the object at
fromoffromTypein the preallocated space attotypedtoType. Returnstrueif the conversion succeeded, otherwise false.