Qt 4.8
Public Types | Static Public Functions | Related Functions | List of all members
QMetaType Class Reference

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

#include <qmetatype.h>

Public Types

typedef void *(* Constructor) (const void *)
 
typedef void(* Destructor) (void *)
 
typedef void(* LoadOperator) (QDataStream &, void *)
 
typedef void(* SaveOperator) (QDataStream &, const void *)
 
enum  Type {
  Void = 0, Bool = 1, Int = 2, UInt = 3,
  LongLong = 4, ULongLong = 5, Double = 6, QChar = 7,
  QVariantMap = 8, QVariantList = 9, QString = 10, QStringList = 11,
  QByteArray = 12, QBitArray = 13, QDate = 14, QTime = 15,
  QDateTime = 16, QUrl = 17, QLocale = 18, QRect = 19,
  QRectF = 20, QSize = 21, QSizeF = 22, QLine = 23,
  QLineF = 24, QPoint = 25, QPointF = 26, QRegExp = 27,
  QVariantHash = 28, QEasingCurve = 29, LastCoreType = QEasingCurve, FirstGuiType = 63,
  QFont = 64, QPixmap = 65, QBrush = 66, QColor = 67,
  QPalette = 68, QIcon = 69, QImage = 70, QPolygon = 71,
  QRegion = 72, QBitmap = 73, QCursor = 74, QSizePolicy = 75,
  QKeySequence = 76, QPen = 77, QTextLength = 78, QTextFormat = 79,
  QMatrix = 80, QTransform = 81, QMatrix4x4 = 82, QVector2D = 83,
  QVector3D = 84, QVector4D = 85, QQuaternion = 86, LastGuiType = QQuaternion,
  FirstCoreExtType = 128, VoidStar = 128, Long = 129, Short = 130,
  Char = 131, ULong = 132, UShort = 133, UChar = 134,
  Float = 135, QObjectStar = 136, QWidgetStar = 137, QVariant = 138,
  LastCoreExtType = QVariant, QReal = Double, User = 256
}
 These are the built-in types supported by QMetaType: More...
 

Static Public Functions

static void * construct (int type, const void *copy=0)
 Returns a copy of copy, assuming it is of type type. More...
 
static void destroy (int type, void *data)
 Destroys the data, assuming it is of the type given. More...
 
static bool isRegistered (int type)
 Returns true if the datatype with ID type is registered; otherwise returns false. More...
 
static bool load (QDataStream &stream, int type, void *data)
 Reads the object of the specified type from the given stream into data. More...
 
static void registerStreamOperators (const char *typeName, SaveOperator saveOp, LoadOperator loadOp)
 
static void registerStreamOperators (int type, SaveOperator saveOp, LoadOperator loadOp)
 
static int registerType (const char *typeName, Destructor destructor, Constructor constructor)
 Registers a user type for marshalling, with typeName, a destructor, and a constructor. More...
 
static int registerTypedef (const char *typeName, int aliasId)
 
static bool save (QDataStream &stream, int type, const void *data)
 Writes the object pointed to by data with the ID type to the given stream. More...
 
static int type (const char *typeName)
 Returns a handle to the type called typeName, or 0 if there is no such type. More...
 
static const char * typeName (int type)
 Returns the type name associated with the given type, or 0 if no matching type was found. More...
 
static void unregisterType (const char *typeName)
 Unregisters a user type, with typeName. More...
 

Related Functions

(Note that these are not member functions.)

int qMetaTypeId ()
 Returns the meta type id of type T at compile time. More...
 
int qRegisterMetaType (const char *typeName)
 
int qRegisterMetaType ()
 Call this function to register the type T. More...
 
int qRegisterMetaTypeStreamOperators (const char *typeName)
 

Detailed Description

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

Note
This class or function is threadsafe.

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. Declare new types with Q_DECLARE_METATYPE() to make them available to QVariant and other template-based functions. Call qRegisterMetaType() to make type available to non-template based functions, such as the queued signal and slot connections.

Any class or struct that has a public default constructor, a public copy constructor, and a public destructor can be registered.

The following code allocates and destructs an instance of MyClass:

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

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 62 of file qmetatype.h.

Typedefs

◆ Constructor

QMetaType::Constructor
Warning
This function is not part of the public interface.

Definition at line 105 of file qmetatype.h.

◆ Destructor

QMetaType::Destructor
Warning
This function is not part of the public interface.

Definition at line 104 of file qmetatype.h.

◆ LoadOperator

QMetaType::LoadOperator
Warning
This function is not part of the public interface.

Definition at line 109 of file qmetatype.h.

◆ SaveOperator

QMetaType::SaveOperator
Warning
This function is not part of the public interface.

Definition at line 108 of file qmetatype.h.

Enumerations

◆ Type

These are the built-in types supported by QMetaType:

  • VoidStar void *
  • Long long
  • LongLong LongLong
  • Short short
  • Char char
  • ULong unsigned long
  • ULongLong ULongLong
  • UShort unsigned short
  • UChar unsigned char
  • Float float
  • QObjectStar QObject *
  • QWidgetStar QWidget *
  • QVariant QVariant
  • User Base value for user types
  • FirstCoreExtType
  • FirstGuiType
  • LastCoreExtType
  • LastCoreType
  • LastGuiType
  • QReal

Additional types can be registered using Q_DECLARE_METATYPE().

See also
type(), typeName()
Enumerator
Void 
Bool 
Int 
UInt 
LongLong 
ULongLong 
Double 
QChar 
QVariantMap 
QVariantList 
QString 
QStringList 
QByteArray 
QBitArray 
QDate 
QTime 
QDateTime 
QUrl 
QLocale 
QRect 
QRectF 
QSize 
QSizeF 
QLine 
QLineF 
QPoint 
QPointF 
QRegExp 
QVariantHash 
QEasingCurve 
LastCoreType 
FirstGuiType 
QFont 
QPixmap 
QBrush 
QColor 
QPalette 
QIcon 
QImage 
QPolygon 
QRegion 
QBitmap 
QCursor 
QSizePolicy 
QKeySequence 
QPen 
QTextLength 
QTextFormat 
QMatrix 
QTransform 
QMatrix4x4 
QVector2D 
QVector3D 
QVector4D 
QQuaternion 
LastGuiType 
FirstCoreExtType 
VoidStar 
Long 
Short 
Char 
ULong 
UShort 
UChar 
Float 
QObjectStar 
QWidgetStar 
QVariant 
LastCoreExtType 
QReal 
User 

Definition at line 64 of file qmetatype.h.

64  {
65  // these are merged with QVariant
66  Void = 0, Bool = 1, Int = 2, UInt = 3, LongLong = 4, ULongLong = 5,
67  Double = 6, QChar = 7, QVariantMap = 8, QVariantList = 9,
68  QString = 10, QStringList = 11, QByteArray = 12,
69  QBitArray = 13, QDate = 14, QTime = 15, QDateTime = 16, QUrl = 17,
70  QLocale = 18, QRect = 19, QRectF = 20, QSize = 21, QSizeF = 22,
71  QLine = 23, QLineF = 24, QPoint = 25, QPointF = 26, QRegExp = 27,
73 
74  FirstGuiType = 63 /* QColorGroup */,
75 #ifdef QT3_SUPPORT
76  QColorGroup = 63,
77 #endif
78  QFont = 64, QPixmap = 65, QBrush = 66, QColor = 67, QPalette = 68,
79  QIcon = 69, QImage = 70, QPolygon = 71, QRegion = 72, QBitmap = 73,
80  QCursor = 74, QSizePolicy = 75, QKeySequence = 76, QPen = 77,
81  QTextLength = 78, QTextFormat = 79, QMatrix = 80, QTransform = 81,
82  QMatrix4x4 = 82, QVector2D = 83, QVector3D = 84, QVector4D = 85,
83  QQuaternion = 86,
85 
86  FirstCoreExtType = 128 /* VoidStar */,
87  VoidStar = 128, Long = 129, Short = 130, Char = 131, ULong = 132,
88  UShort = 133, UChar = 134, Float = 135, QObjectStar = 136, QWidgetStar = 137,
89  QVariant = 138,
91 
92 // This logic must match the one in qglobal.h
93 #if defined(QT_COORD_TYPE)
94  QReal = 0,
95 #elif defined(QT_NO_FPU) || defined(QT_ARCH_ARM) || defined(QT_ARCH_WINDOWSCE) || defined(QT_ARCH_SYMBIAN)
96  QReal = Float,
97 #else
98  QReal = Double,
99 #endif
100 
101  User = 256
102  };
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
The QVector3D class represents a vector or vertex in 3D space.
Definition: qvector3d.h:60
The QCursor class provides a mouse cursor with an arbitrary shape.
Definition: qcursor.h:89
The QEasingCurve class provides easing curves for controlling animation.
Definition: qeasingcurve.h:55
The QLine class provides a two-dimensional vector using integer precision.
Definition: qline.h:57
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
The QMatrix class specifies 2D transformations of a coordinate system.
Definition: qmatrix.h:61
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QVector4D class represents a vector or vertex in 4D space.
Definition: qvector4d.h:60
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
The QDate class provides date functions.
Definition: qdatetime.h:55
The QPolygon class provides a vector of points using integer precision.
Definition: qpolygon.h:60
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QPen class defines how a QPainter should draw lines and outlines of shapes.
Definition: qpen.h:64
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
The QLineF class provides a two-dimensional vector using floating point precision.
Definition: qline.h:212
The QBitmap class provides monochrome (1-bit depth) pixmaps.
Definition: qbitmap.h:55
The QTime class provides clock time functions.
Definition: qdatetime.h:148
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
The QTextFormat class provides formatting information for a QTextDocument.
Definition: qtextformat.h:129
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition: qmatrix4x4.h:63
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
The QVector2D class represents a vector or vertex in 2D space.
Definition: qvector2d.h:60
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
ushort Char
The QBitArray class provides an array of bits.
Definition: qbitarray.h:54
The QBrush class defines the fill pattern of shapes drawn by QPainter.
Definition: qbrush.h:76
The QKeySequence class encapsulates a key sequence as used by shortcuts.
Definition: qkeysequence.h:72
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
The QTextLength class encapsulates the different types of length used in a QTextDocument.
Definition: qtextformat.h:84
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
The QColorGroup class contains color groups for each widget state.
The QQuaternion class represents a quaternion consisting of a vector and scalar.
Definition: qquaternion.h:59
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:65
The QPalette class contains color groups for each widget state.
Definition: qpalette.h:61
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60

Functions

◆ construct()

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

Returns a copy of copy, assuming it is of type type.

If copy is zero, creates a default type.

See also
destroy(), isRegistered(), Type

Definition at line 1042 of file qmetatype.cpp.

Referenced by QTestData::append(), construct(), QMetaMethod::invoke(), qax_createObjectWrapper(), and queued_activate().

1043 {
1044  if (copy) {
1045  switch(type) {
1046  case QMetaType::VoidStar:
1049  return new void *(*static_cast<void* const *>(copy));
1050  case QMetaType::Long:
1051  return new long(*static_cast<const long*>(copy));
1052  case QMetaType::Int:
1053  return new int(*static_cast<const int*>(copy));
1054  case QMetaType::Short:
1055  return new short(*static_cast<const short*>(copy));
1056  case QMetaType::Char:
1057  return new char(*static_cast<const char*>(copy));
1058  case QMetaType::ULong:
1059  return new ulong(*static_cast<const ulong*>(copy));
1060  case QMetaType::UInt:
1061  return new uint(*static_cast<const uint*>(copy));
1062  case QMetaType::LongLong:
1063  return new qlonglong(*static_cast<const qlonglong*>(copy));
1064  case QMetaType::ULongLong:
1065  return new qulonglong(*static_cast<const qulonglong*>(copy));
1066  case QMetaType::UShort:
1067  return new ushort(*static_cast<const ushort*>(copy));
1068  case QMetaType::UChar:
1069  return new uchar(*static_cast<const uchar*>(copy));
1070  case QMetaType::Bool:
1071  return new bool(*static_cast<const bool*>(copy));
1072  case QMetaType::Float:
1073  return new float(*static_cast<const float*>(copy));
1074  case QMetaType::Double:
1075  return new double(*static_cast<const double*>(copy));
1076  case QMetaType::QChar:
1077  return new NS(QChar)(*static_cast<const NS(QChar)*>(copy));
1078 #ifndef QT_BOOTSTRAPPED
1080  return new NS(QVariantMap)(*static_cast<const NS(QVariantMap)*>(copy));
1082  return new NS(QVariantHash)(*static_cast<const NS(QVariantHash)*>(copy));
1084  return new NS(QVariantList)(*static_cast<const NS(QVariantList)*>(copy));
1085  case QMetaType::QVariant:
1086  return new NS(QVariant)(*static_cast<const NS(QVariant)*>(copy));
1087 #endif
1088  case QMetaType::QByteArray:
1089  return new NS(QByteArray)(*static_cast<const NS(QByteArray)*>(copy));
1090  case QMetaType::QString:
1091  return new NS(QString)(*static_cast<const NS(QString)*>(copy));
1093  return new NS(QStringList)(*static_cast<const NS(QStringList)*>(copy));
1094 #ifndef QT_BOOTSTRAPPED
1095  case QMetaType::QBitArray:
1096  return new NS(QBitArray)(*static_cast<const NS(QBitArray)*>(copy));
1097 #endif
1098  case QMetaType::QDate:
1099  return new NS(QDate)(*static_cast<const NS(QDate)*>(copy));
1100  case QMetaType::QTime:
1101  return new NS(QTime)(*static_cast<const NS(QTime)*>(copy));
1102  case QMetaType::QDateTime:
1103  return new NS(QDateTime)(*static_cast<const NS(QDateTime)*>(copy));
1104 #ifndef QT_BOOTSTRAPPED
1105  case QMetaType::QUrl:
1106  return new NS(QUrl)(*static_cast<const NS(QUrl)*>(copy));
1107 #endif
1108  case QMetaType::QLocale:
1109  return new NS(QLocale)(*static_cast<const NS(QLocale)*>(copy));
1110 #ifndef QT_NO_GEOM_VARIANT
1111  case QMetaType::QRect:
1112  return new NS(QRect)(*static_cast<const NS(QRect)*>(copy));
1113  case QMetaType::QRectF:
1114  return new NS(QRectF)(*static_cast<const NS(QRectF)*>(copy));
1115  case QMetaType::QSize:
1116  return new NS(QSize)(*static_cast<const NS(QSize)*>(copy));
1117  case QMetaType::QSizeF:
1118  return new NS(QSizeF)(*static_cast<const NS(QSizeF)*>(copy));
1119  case QMetaType::QLine:
1120  return new NS(QLine)(*static_cast<const NS(QLine)*>(copy));
1121  case QMetaType::QLineF:
1122  return new NS(QLineF)(*static_cast<const NS(QLineF)*>(copy));
1123  case QMetaType::QPoint:
1124  return new NS(QPoint)(*static_cast<const NS(QPoint)*>(copy));
1125  case QMetaType::QPointF:
1126  return new NS(QPointF)(*static_cast<const NS(QPointF)*>(copy));
1127 #endif
1128 #ifndef QT_NO_REGEXP
1129  case QMetaType::QRegExp:
1130  return new NS(QRegExp)(*static_cast<const NS(QRegExp)*>(copy));
1131 #endif
1132 #ifndef QT_BOOTSTRAPPED
1134  return new NS(QEasingCurve)(*static_cast<const NS(QEasingCurve)*>(copy));
1135 #endif
1136  case QMetaType::Void:
1137  return 0;
1138  default:
1139  ;
1140  }
1141  } else {
1142  switch(type) {
1143  case QMetaType::VoidStar:
1146  return new void *;
1147  case QMetaType::Long:
1148  return new long;
1149  case QMetaType::Int:
1150  return new int;
1151  case QMetaType::Short:
1152  return new short;
1153  case QMetaType::Char:
1154  return new char;
1155  case QMetaType::ULong:
1156  return new ulong;
1157  case QMetaType::UInt:
1158  return new uint;
1159  case QMetaType::LongLong:
1160  return new qlonglong;
1161  case QMetaType::ULongLong:
1162  return new qulonglong;
1163  case QMetaType::UShort:
1164  return new ushort;
1165  case QMetaType::UChar:
1166  return new uchar;
1167  case QMetaType::Bool:
1168  return new bool;
1169  case QMetaType::Float:
1170  return new float;
1171  case QMetaType::Double:
1172  return new double;
1173  case QMetaType::QChar:
1174  return new NS(QChar);
1175 #ifndef QT_BOOTSTRAPPED
1177  return new NS(QVariantMap);
1179  return new NS(QVariantHash);
1181  return new NS(QVariantList);
1182  case QMetaType::QVariant:
1183  return new NS(QVariant);
1184 #endif
1185  case QMetaType::QByteArray:
1186  return new NS(QByteArray);
1187  case QMetaType::QString:
1188  return new NS(QString);
1190  return new NS(QStringList);
1191 #ifndef QT_BOOTSTRAPPED
1192  case QMetaType::QBitArray:
1193  return new NS(QBitArray);
1194 #endif
1195  case QMetaType::QDate:
1196  return new NS(QDate);
1197  case QMetaType::QTime:
1198  return new NS(QTime);
1199  case QMetaType::QDateTime:
1200  return new NS(QDateTime);
1201 #ifndef QT_BOOTSTRAPPED
1202  case QMetaType::QUrl:
1203  return new NS(QUrl);
1204 #endif
1205  case QMetaType::QLocale:
1206  return new NS(QLocale);
1207 #ifndef QT_NO_GEOM_VARIANT
1208  case QMetaType::QRect:
1209  return new NS(QRect);
1210  case QMetaType::QRectF:
1211  return new NS(QRectF);
1212  case QMetaType::QSize:
1213  return new NS(QSize);
1214  case QMetaType::QSizeF:
1215  return new NS(QSizeF);
1216  case QMetaType::QLine:
1217  return new NS(QLine);
1218  case QMetaType::QLineF:
1219  return new NS(QLineF);
1220  case QMetaType::QPoint:
1221  return new NS(QPoint);
1222  case QMetaType::QPointF:
1223  return new NS(QPointF);
1224 #endif
1225 #ifndef QT_NO_REGEXP
1226  case QMetaType::QRegExp:
1227  return new NS(QRegExp);
1228 #endif
1229 #ifndef QT_BOOTSTRAPPED
1231  return new NS(QEasingCurve);
1232 #endif
1233  case QMetaType::Void:
1234  return 0;
1235  default:
1236  ;
1237  }
1238  }
1239 
1240  Constructor constr = 0;
1241  if (type >= FirstGuiType && type <= LastGuiType) {
1242  if (!qMetaTypeGuiHelper)
1243  return 0;
1245  } else {
1246  const QVector<QCustomTypeInfo> * const ct = customTypes();
1247  QReadLocker locker(customTypesLock());
1248  if (type < User || !ct || ct->count() <= type - User)
1249  return 0;
1250  if (ct->at(type - User).typeName.isEmpty())
1251  return 0;
1252  constr = ct->at(type - User).constr;
1253  }
1254 
1255  return constr(copy);
1256 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
const QStringList & customTypes
The QEasingCurve class provides easing curves for controlling animation.
Definition: qeasingcurve.h:55
The QLine class provides a two-dimensional vector using integer precision.
Definition: qline.h:57
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QMetaType::Constructor constr
Definition: qmetatype.cpp:339
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
void *(* Constructor)(const void *)
Definition: qmetatype.h:105
The QDate class provides date functions.
Definition: qdatetime.h:55
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
The QLineF class provides a two-dimensional vector using floating point precision.
Definition: qline.h:212
QByteArray typeName
Definition: qmetatype.cpp:357
unsigned char uchar
Definition: qglobal.h:994
The QTime class provides clock time functions.
Definition: qdatetime.h:148
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
Q_CORE_EXPORT const QMetaTypeGuiHelper * qMetaTypeGuiHelper
Definition: qmetatype.cpp:346
unsigned int uint
Definition: qglobal.h:996
static int type(const char *typeName)
Returns a handle to the type called typeName, or 0 if there is no such type.
Definition: qmetatype.cpp:607
The QReadLocker class is a convenience class that simplifies locking and unlocking read-write locks f...
unsigned long ulong
Definition: qglobal.h:997
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
The QBitArray class provides an array of bits.
Definition: qbitarray.h:54
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
unsigned short ushort
Definition: qglobal.h:995
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
#define NS(x)
Definition: qmetatype.cpp:72
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
quint64 qulonglong
Definition: qglobal.h:952
qint64 qlonglong
Definition: qglobal.h:951
QMetaType::Constructor constr
Definition: qmetatype.cpp:358

◆ destroy()

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

Destroys the data, assuming it is of the type given.

See also
construct(), isRegistered(), Type

Definition at line 1263 of file qmetatype.cpp.

Referenced by clear(), QMetaMethod::invoke(), QMetaCallEvent::~QMetaCallEvent(), and QTestData::~QTestData().

1264 {
1265  if (!data)
1266  return;
1267  switch(type) {
1268  case QMetaType::VoidStar:
1271  delete static_cast<void**>(data);
1272  break;
1273  case QMetaType::Long:
1274  delete static_cast<long*>(data);
1275  break;
1276  case QMetaType::Int:
1277  delete static_cast<int*>(data);
1278  break;
1279  case QMetaType::Short:
1280  delete static_cast<short*>(data);
1281  break;
1282  case QMetaType::Char:
1283  delete static_cast<char*>(data);
1284  break;
1285  case QMetaType::ULong:
1286  delete static_cast<ulong*>(data);
1287  break;
1288  case QMetaType::LongLong:
1289  delete static_cast<qlonglong*>(data);
1290  break;
1291  case QMetaType::ULongLong:
1292  delete static_cast<qulonglong*>(data);
1293  break;
1294  case QMetaType::UInt:
1295  delete static_cast<uint*>(data);
1296  break;
1297  case QMetaType::UShort:
1298  delete static_cast<ushort*>(data);
1299  break;
1300  case QMetaType::UChar:
1301  delete static_cast<uchar*>(data);
1302  break;
1303  case QMetaType::Bool:
1304  delete static_cast<bool*>(data);
1305  break;
1306  case QMetaType::Float:
1307  delete static_cast<float*>(data);
1308  break;
1309  case QMetaType::Double:
1310  delete static_cast<double*>(data);
1311  break;
1312  case QMetaType::QChar:
1313  delete static_cast< NS(QChar)* >(data);
1314  break;
1315 #ifndef QT_BOOTSTRAPPED
1317  delete static_cast< NS(QVariantMap)* >(data);
1318  break;
1320  delete static_cast< NS(QVariantHash)* >(data);
1321  break;
1323  delete static_cast< NS(QVariantList)* >(data);
1324  break;
1325  case QMetaType::QVariant:
1326  delete static_cast< NS(QVariant)* >(data);
1327  break;
1328 #endif
1329  case QMetaType::QByteArray:
1330  delete static_cast< NS(QByteArray)* >(data);
1331  break;
1332  case QMetaType::QString:
1333  delete static_cast< NS(QString)* >(data);
1334  break;
1336  delete static_cast< NS(QStringList)* >(data);
1337  break;
1338 #ifndef QT_BOOTSTRAPPED
1339  case QMetaType::QBitArray:
1340  delete static_cast< NS(QBitArray)* >(data);
1341  break;
1342 #endif
1343  case QMetaType::QDate:
1344  delete static_cast< NS(QDate)* >(data);
1345  break;
1346  case QMetaType::QTime:
1347  delete static_cast< NS(QTime)* >(data);
1348  break;
1349  case QMetaType::QDateTime:
1350  delete static_cast< NS(QDateTime)* >(data);
1351  break;
1352 #ifndef QT_BOOTSTRAPPED
1353  case QMetaType::QUrl:
1354  delete static_cast< NS(QUrl)* >(data);
1355 #endif
1356  break;
1357  case QMetaType::QLocale:
1358  delete static_cast< NS(QLocale)* >(data);
1359  break;
1360 #ifndef QT_NO_GEOM_VARIANT
1361  case QMetaType::QRect:
1362  delete static_cast< NS(QRect)* >(data);
1363  break;
1364  case QMetaType::QRectF:
1365  delete static_cast< NS(QRectF)* >(data);
1366  break;
1367  case QMetaType::QSize:
1368  delete static_cast< NS(QSize)* >(data);
1369  break;
1370  case QMetaType::QSizeF:
1371  delete static_cast< NS(QSizeF)* >(data);
1372  break;
1373  case QMetaType::QLine:
1374  delete static_cast< NS(QLine)* >(data);
1375  break;
1376  case QMetaType::QLineF:
1377  delete static_cast< NS(QLineF)* >(data);
1378  break;
1379  case QMetaType::QPoint:
1380  delete static_cast< NS(QPoint)* >(data);
1381  break;
1382  case QMetaType::QPointF:
1383  delete static_cast< NS(QPointF)* >(data);
1384  break;
1385 #endif
1386 #ifndef QT_NO_REGEXP
1387  case QMetaType::QRegExp:
1388  delete static_cast< NS(QRegExp)* >(data);
1389  break;
1390 #endif
1391 #ifndef QT_BOOTSTRAPPED
1393  delete static_cast< NS(QEasingCurve)* >(data);
1394  break;
1395 #endif
1396  case QMetaType::Void:
1397  break;
1398  default: {
1399  const QVector<QCustomTypeInfo> * const ct = customTypes();
1400  Destructor destr = 0;
1401  if (type >= FirstGuiType && type <= LastGuiType) {
1403 
1404  if (!qMetaTypeGuiHelper)
1405  return;
1407  } else {
1408  QReadLocker locker(customTypesLock());
1409  if (type < User || !ct || ct->count() <= type - User)
1410  break;
1411  if (ct->at(type - User).typeName.isEmpty())
1412  break;
1413  destr = ct->at(type - User).destr;
1414  }
1415  destr(data);
1416  break; }
1417  }
1418 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
const QStringList & customTypes
The QEasingCurve class provides easing curves for controlling animation.
Definition: qeasingcurve.h:55
The QLine class provides a two-dimensional vector using integer precision.
Definition: qline.h:57
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
The QDate class provides date functions.
Definition: qdatetime.h:55
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
void(* Destructor)(void *)
Definition: qmetatype.h:104
The QLineF class provides a two-dimensional vector using floating point precision.
Definition: qline.h:212
QByteArray typeName
Definition: qmetatype.cpp:357
unsigned char uchar
Definition: qglobal.h:994
The QTime class provides clock time functions.
Definition: qdatetime.h:148
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
Q_CORE_EXPORT const QMetaTypeGuiHelper * qMetaTypeGuiHelper
Definition: qmetatype.cpp:346
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
static int type(const char *typeName)
Returns a handle to the type called typeName, or 0 if there is no such type.
Definition: qmetatype.cpp:607
The QReadLocker class is a convenience class that simplifies locking and unlocking read-write locks f...
unsigned long ulong
Definition: qglobal.h:997
QMetaType::Destructor destr
Definition: qmetatype.cpp:359
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
The QBitArray class provides an array of bits.
Definition: qbitarray.h:54
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
unsigned short ushort
Definition: qglobal.h:995
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
QMetaType::Destructor destr
Definition: qmetatype.cpp:340
#define NS(x)
Definition: qmetatype.cpp:72
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
quint64 qulonglong
Definition: qglobal.h:952
qint64 qlonglong
Definition: qglobal.h:951

◆ isRegistered()

bool QMetaType::isRegistered ( int  type)
static

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

See also
type(), typeName(), Type

Definition at line 590 of file qmetatype.cpp.

Referenced by compare().

591 {
592  if (type >= 0 && type < User) {
593  // predefined type
594  return true;
595  }
596  QReadLocker locker(customTypesLock());
597  const QVector<QCustomTypeInfo> * const ct = customTypes();
598  return ((type >= User) && (ct && ct->count() > type - User) && !ct->at(type - User).typeName.isEmpty());
599 }
const QStringList & customTypes
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
QByteArray typeName
Definition: qmetatype.cpp:357
static int type(const char *typeName)
Returns a handle to the type called typeName, or 0 if there is no such type.
Definition: qmetatype.cpp:607
The QReadLocker class is a convenience class that simplifies locking and unlocking read-write locks f...
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421

◆ load()

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

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

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

The type must have been registered with qRegisterMetaType() and qRegisterMetaTypeStreamOperators() beforehand.

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(), qRegisterMetaTypeStreamOperators()

Definition at line 845 of file qmetatype.cpp.

Referenced by QVariant::load().

846 {
847  if (!data || !isRegistered(type))
848  return false;
849 
850  switch(type) {
851  case QMetaType::Void:
852  case QMetaType::VoidStar:
855  return false;
856  case QMetaType::Long: {
857  qlonglong l;
858  stream >> l;
859  *static_cast<long *>(data) = long(l);
860  break; }
861  case QMetaType::Int:
862  stream >> *static_cast<int *>(data);
863  break;
864  case QMetaType::Short:
865  stream >> *static_cast<short *>(data);
866  break;
867  case QMetaType::Char:
868  // force a char to be signed
869  stream >> *static_cast<signed char *>(data);
870  break;
871  case QMetaType::ULong: {
872  qulonglong ul;
873  stream >> ul;
874  *static_cast<ulong *>(data) = ulong(ul);
875  break; }
876  case QMetaType::UInt:
877  stream >> *static_cast<uint *>(data);
878  break;
879  case QMetaType::LongLong:
880  stream >> *static_cast<qlonglong *>(data);
881  break;
883  stream >> *static_cast<qulonglong *>(data);
884  break;
885  case QMetaType::UShort:
886  stream >> *static_cast<ushort *>(data);
887  break;
888  case QMetaType::UChar:
889  stream >> *static_cast<uchar *>(data);
890  break;
891  case QMetaType::Bool: {
892  qint8 b;
893  stream >> b;
894  *static_cast<bool *>(data) = b;
895  break; }
896  case QMetaType::Float:
897  stream >> *static_cast<float *>(data);
898  break;
899  case QMetaType::Double:
900  stream >> *static_cast<double *>(data);
901  break;
902  case QMetaType::QChar:
903  stream >> *static_cast< NS(QChar)*>(data);
904  break;
905 #ifndef QT_BOOTSTRAPPED
907  stream >> *static_cast< NS(QVariantMap)*>(data);
908  break;
910  stream >> *static_cast< NS(QVariantHash)*>(data);
911  break;
913  stream >> *static_cast< NS(QVariantList)*>(data);
914  break;
915  case QMetaType::QVariant:
916  stream >> *static_cast< NS(QVariant)*>(data);
917  break;
918 #endif
920  stream >> *static_cast< NS(QByteArray)*>(data);
921  break;
922  case QMetaType::QString:
923  stream >> *static_cast< NS(QString)*>(data);
924  break;
926  stream >> *static_cast< NS(QStringList)*>(data);
927  break;
928 #ifndef QT_BOOTSTRAPPED
930  stream >> *static_cast< NS(QBitArray)*>(data);
931  break;
932 #endif
933  case QMetaType::QDate:
934  stream >> *static_cast< NS(QDate)*>(data);
935  break;
936  case QMetaType::QTime:
937  stream >> *static_cast< NS(QTime)*>(data);
938  break;
940  stream >> *static_cast< NS(QDateTime)*>(data);
941  break;
942 #ifndef QT_BOOTSTRAPPED
943  case QMetaType::QUrl:
944  stream >> *static_cast< NS(QUrl)*>(data);
945  break;
946 #endif
947  case QMetaType::QLocale:
948  stream >> *static_cast< NS(QLocale)*>(data);
949  break;
950 #ifndef QT_NO_GEOM_VARIANT
951  case QMetaType::QRect:
952  stream >> *static_cast< NS(QRect)*>(data);
953  break;
954  case QMetaType::QRectF:
955  stream >> *static_cast< NS(QRectF)*>(data);
956  break;
957  case QMetaType::QSize:
958  stream >> *static_cast< NS(QSize)*>(data);
959  break;
960  case QMetaType::QSizeF:
961  stream >> *static_cast< NS(QSizeF)*>(data);
962  break;
963  case QMetaType::QLine:
964  stream >> *static_cast< NS(QLine)*>(data);
965  break;
966  case QMetaType::QLineF:
967  stream >> *static_cast< NS(QLineF)*>(data);
968  break;
969  case QMetaType::QPoint:
970  stream >> *static_cast< NS(QPoint)*>(data);
971  break;
972  case QMetaType::QPointF:
973  stream >> *static_cast< NS(QPointF)*>(data);
974  break;
975 #endif
976 #ifndef QT_NO_REGEXP
977  case QMetaType::QRegExp:
978  stream >> *static_cast< NS(QRegExp)*>(data);
979  break;
980 #endif
981 #ifndef QT_BOOTSTRAPPED
983  stream >> *static_cast< NS(QEasingCurve)*>(data);
984  break;
985 #endif
986 #ifdef QT3_SUPPORT
987  case QMetaType::QColorGroup:
988 #endif
989  case QMetaType::QFont:
990  case QMetaType::QPixmap:
991  case QMetaType::QBrush:
992  case QMetaType::QColor:
993  case QMetaType::QPalette:
994  case QMetaType::QIcon:
995  case QMetaType::QImage:
996  case QMetaType::QPolygon:
997  case QMetaType::QRegion:
998  case QMetaType::QBitmap:
999  case QMetaType::QCursor:
1002  case QMetaType::QPen:
1005  case QMetaType::QMatrix:
1006  case QMetaType::QTransform:
1007  case QMetaType::QMatrix4x4:
1008  case QMetaType::QVector2D:
1009  case QMetaType::QVector3D:
1010  case QMetaType::QVector4D:
1012  if (!qMetaTypeGuiHelper)
1013  return false;
1015  break;
1016  default: {
1017  const QVector<QCustomTypeInfo> * const ct = customTypes();
1018  if (!ct)
1019  return false;
1020 
1021  LoadOperator loadOp = 0;
1022  {
1023  QReadLocker locker(customTypesLock());
1024  loadOp = ct->at(type - User).loadOp;
1025  }
1026 
1027  if (!loadOp)
1028  return false;
1029  loadOp(stream, data);
1030  break; }
1031  }
1032  return true;
1033 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
const QStringList & customTypes
The QEasingCurve class provides easing curves for controlling animation.
Definition: qeasingcurve.h:55
The QLine class provides a two-dimensional vector using integer precision.
Definition: qline.h:57
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
The QDate class provides date functions.
Definition: qdatetime.h:55
void(* LoadOperator)(QDataStream &, void *)
Definition: qmetatype.h:109
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
The QLineF class provides a two-dimensional vector using floating point precision.
Definition: qline.h:212
signed char qint8
Definition: qglobal.h:933
unsigned char uchar
Definition: qglobal.h:994
The QTime class provides clock time functions.
Definition: qdatetime.h:148
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
Q_CORE_EXPORT const QMetaTypeGuiHelper * qMetaTypeGuiHelper
Definition: qmetatype.cpp:346
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
static int type(const char *typeName)
Returns a handle to the type called typeName, or 0 if there is no such type.
Definition: qmetatype.cpp:607
The QReadLocker class is a convenience class that simplifies locking and unlocking read-write locks f...
unsigned long ulong
Definition: qglobal.h:997
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
The QBitArray class provides an array of bits.
Definition: qbitarray.h:54
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
unsigned short ushort
Definition: qglobal.h:995
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
QFactoryLoader * l
#define NS(x)
Definition: qmetatype.cpp:72
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QMetaType::LoadOperator loadOp
Definition: qmetatype.cpp:343
quint64 qulonglong
Definition: qglobal.h:952
QMetaType::LoadOperator loadOp
Definition: qmetatype.cpp:362
qint64 qlonglong
Definition: qglobal.h:951
static bool isRegistered(int type)
Returns true if the datatype with ID type is registered; otherwise returns false. ...
Definition: qmetatype.cpp:590

◆ registerStreamOperators() [1/2]

void QMetaType::registerStreamOperators ( const char *  typeName,
SaveOperator  saveOp,
LoadOperator  loadOp 
)
static
Warning
This function is not part of the public interface.

Definition at line 374 of file qmetatype.cpp.

376 {
377  int idx = type(typeName);
378  if (!idx)
379  return;
380  registerStreamOperators(idx, saveOp, loadOp);
381 }
static void registerStreamOperators(const char *typeName, SaveOperator saveOp, LoadOperator loadOp)
Definition: qmetatype.cpp:374
static int type(const char *typeName)
Returns a handle to the type called typeName, or 0 if there is no such type.
Definition: qmetatype.cpp:607
static const char * typeName(int type)
Returns the type name associated with the given type, or 0 if no matching type was found...
Definition: qmetatype.cpp:406

◆ registerStreamOperators() [2/2]

void QMetaType::registerStreamOperators ( int  idx,
SaveOperator  saveOp,
LoadOperator  loadOp 
)
static
Warning
This function is not part of the public interface.

Definition at line 385 of file qmetatype.cpp.

387 {
388  if (idx < User)
389  return; //builtin types should not be registered;
391  if (!ct)
392  return;
393  QWriteLocker locker(customTypesLock());
394  QCustomTypeInfo &inf = (*ct)[idx - User];
395  inf.saveOp = saveOp;
396  inf.loadOp = loadOp;
397 }
const QStringList & customTypes
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
The QWriteLocker class is a convenience class that simplifies locking and unlocking read-write locks ...
QMetaType::LoadOperator loadOp
Definition: qmetatype.cpp:362
QMetaType::SaveOperator saveOp
Definition: qmetatype.cpp:361

◆ registerType()

int QMetaType::registerType ( const char *  typeName,
Destructor  destructor,
Constructor  constructor 
)
static

Registers a user type for marshalling, with typeName, a destructor, and a constructor.

Warning
This function is not part of the public interface.

Returns the type's handle, or -1 if the type could not be registered.

Definition at line 477 of file qmetatype.cpp.

Referenced by QDeclarativeEnginePrivate::registerCompositeType().

479 {
481  if (!ct || !typeName || !destructor || !constructor)
482  return -1;
483 
484 #ifdef QT_NO_QOBJECT
485  NS(QByteArray) normalizedTypeName = typeName;
486 #else
487  NS(QByteArray) normalizedTypeName = QMetaObject::normalizedType(typeName);
488 #endif
489 
490  int idx = qMetaTypeStaticType(normalizedTypeName.constData(),
491  normalizedTypeName.size());
492 
493  if (!idx) {
494  QWriteLocker locker(customTypesLock());
495  idx = qMetaTypeCustomType_unlocked(normalizedTypeName.constData(),
496  normalizedTypeName.size());
497  if (!idx) {
498  QCustomTypeInfo inf;
499  inf.typeName = normalizedTypeName;
500  inf.constr = constructor;
501  inf.destr = destructor;
502  inf.alias = -1;
503  idx = ct->size() + User;
504  ct->append(inf);
505  }
506  }
507  return idx;
508 }
const QStringList & customTypes
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static int qMetaTypeStaticType(const char *typeName, int length)
Similar to QMetaType::type(), but only looks in the static set of types.
Definition: qmetatype.cpp:433
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
QByteArray typeName
Definition: qmetatype.cpp:357
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
QMetaType::Destructor destr
Definition: qmetatype.cpp:359
The QWriteLocker class is a convenience class that simplifies locking and unlocking read-write locks ...
static int qMetaTypeCustomType_unlocked(const char *typeName, int length)
Similar to QMetaType::type(), but only looks in the custom set of types, and doesn&#39;t lock the mutex...
Definition: qmetatype.cpp:450
static const char * typeName(int type)
Returns the type name associated with the given type, or 0 if no matching type was found...
Definition: qmetatype.cpp:406
static QByteArray normalizedType(const char *type)
Normalizes a type.
#define NS(x)
Definition: qmetatype.cpp:72
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
QMetaType::Constructor constr
Definition: qmetatype.cpp:358

◆ registerTypedef()

int QMetaType::registerTypedef ( const char *  typeName,
int  aliasId 
)
static
Warning
This function is not part of the public interface.
Since
4.7

Registers a user type for marshalling, as an alias of another type (typedef)

Definition at line 515 of file qmetatype.cpp.

Referenced by qRegisterMetaType().

516 {
518  if (!ct || !typeName)
519  return -1;
520 
521 #ifdef QT_NO_QOBJECT
522  NS(QByteArray) normalizedTypeName = typeName;
523 #else
524  NS(QByteArray) normalizedTypeName = QMetaObject::normalizedType(typeName);
525 #endif
526 
527  int idx = qMetaTypeStaticType(normalizedTypeName.constData(),
528  normalizedTypeName.size());
529 
530  if (idx) {
531  Q_ASSERT(idx == aliasId);
532  return idx;
533  }
534 
535  QWriteLocker locker(customTypesLock());
536  idx = qMetaTypeCustomType_unlocked(normalizedTypeName.constData(),
537  normalizedTypeName.size());
538 
539  if (idx)
540  return idx;
541 
542  QCustomTypeInfo inf;
543  inf.typeName = normalizedTypeName;
544  inf.alias = aliasId;
545  inf.constr = 0;
546  inf.destr = 0;
547  ct->append(inf);
548  return aliasId;
549 }
const QStringList & customTypes
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static int qMetaTypeStaticType(const char *typeName, int length)
Similar to QMetaType::type(), but only looks in the static set of types.
Definition: qmetatype.cpp:433
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
QByteArray typeName
Definition: qmetatype.cpp:357
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
QMetaType::Destructor destr
Definition: qmetatype.cpp:359
The QWriteLocker class is a convenience class that simplifies locking and unlocking read-write locks ...
static int qMetaTypeCustomType_unlocked(const char *typeName, int length)
Similar to QMetaType::type(), but only looks in the custom set of types, and doesn&#39;t lock the mutex...
Definition: qmetatype.cpp:450
static const char * typeName(int type)
Returns the type name associated with the given type, or 0 if no matching type was found...
Definition: qmetatype.cpp:406
static QByteArray normalizedType(const char *type)
Normalizes a type.
#define NS(x)
Definition: qmetatype.cpp:72
QMetaType::Constructor constr
Definition: qmetatype.cpp:358

◆ save()

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

Writes the object pointed to by data with the ID type to the given stream.

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

The type must have been registered with qRegisterMetaType() and qRegisterMetaTypeStreamOperators() beforehand.

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(), qRegisterMetaTypeStreamOperators()

Definition at line 646 of file qmetatype.cpp.

Referenced by QVariant::save().

647 {
648  if (!data || !isRegistered(type))
649  return false;
650 
651  switch(type) {
652  case QMetaType::Void:
653  case QMetaType::VoidStar:
656  return false;
657  case QMetaType::Long:
658  stream << qlonglong(*static_cast<const long *>(data));
659  break;
660  case QMetaType::Int:
661  stream << *static_cast<const int *>(data);
662  break;
663  case QMetaType::Short:
664  stream << *static_cast<const short *>(data);
665  break;
666  case QMetaType::Char:
667  // force a char to be signed
668  stream << *static_cast<const signed char *>(data);
669  break;
670  case QMetaType::ULong:
671  stream << qulonglong(*static_cast<const ulong *>(data));
672  break;
673  case QMetaType::UInt:
674  stream << *static_cast<const uint *>(data);
675  break;
676  case QMetaType::LongLong:
677  stream << *static_cast<const qlonglong *>(data);
678  break;
680  stream << *static_cast<const qulonglong *>(data);
681  break;
682  case QMetaType::UShort:
683  stream << *static_cast<const ushort *>(data);
684  break;
685  case QMetaType::UChar:
686  stream << *static_cast<const uchar *>(data);
687  break;
688  case QMetaType::Bool:
689  stream << qint8(*static_cast<const bool *>(data));
690  break;
691  case QMetaType::Float:
692  stream << *static_cast<const float *>(data);
693  break;
694  case QMetaType::Double:
695  stream << *static_cast<const double *>(data);
696  break;
697  case QMetaType::QChar:
698  stream << *static_cast<const NS(QChar) *>(data);
699  break;
700 #ifndef QT_BOOTSTRAPPED
702  stream << *static_cast<const NS(QVariantMap)*>(data);
703  break;
705  stream << *static_cast<const NS(QVariantHash)*>(data);
706  break;
708  stream << *static_cast<const NS(QVariantList)*>(data);
709  break;
710  case QMetaType::QVariant:
711  stream << *static_cast<const NS(QVariant)*>(data);
712  break;
713 #endif
715  stream << *static_cast<const NS(QByteArray)*>(data);
716  break;
717  case QMetaType::QString:
718  stream << *static_cast<const NS(QString)*>(data);
719  break;
721  stream << *static_cast<const NS(QStringList)*>(data);
722  break;
723 #ifndef QT_BOOTSTRAPPED
725  stream << *static_cast<const NS(QBitArray)*>(data);
726  break;
727 #endif
728  case QMetaType::QDate:
729  stream << *static_cast<const NS(QDate)*>(data);
730  break;
731  case QMetaType::QTime:
732  stream << *static_cast<const NS(QTime)*>(data);
733  break;
735  stream << *static_cast<const NS(QDateTime)*>(data);
736  break;
737 #ifndef QT_BOOTSTRAPPED
738  case QMetaType::QUrl:
739  stream << *static_cast<const NS(QUrl)*>(data);
740  break;
741 #endif
742  case QMetaType::QLocale:
743  stream << *static_cast<const NS(QLocale)*>(data);
744  break;
745 #ifndef QT_NO_GEOM_VARIANT
746  case QMetaType::QRect:
747  stream << *static_cast<const NS(QRect)*>(data);
748  break;
749  case QMetaType::QRectF:
750  stream << *static_cast<const NS(QRectF)*>(data);
751  break;
752  case QMetaType::QSize:
753  stream << *static_cast<const NS(QSize)*>(data);
754  break;
755  case QMetaType::QSizeF:
756  stream << *static_cast<const NS(QSizeF)*>(data);
757  break;
758  case QMetaType::QLine:
759  stream << *static_cast<const NS(QLine)*>(data);
760  break;
761  case QMetaType::QLineF:
762  stream << *static_cast<const NS(QLineF)*>(data);
763  break;
764  case QMetaType::QPoint:
765  stream << *static_cast<const NS(QPoint)*>(data);
766  break;
767  case QMetaType::QPointF:
768  stream << *static_cast<const NS(QPointF)*>(data);
769  break;
770 #endif
771 #ifndef QT_NO_REGEXP
772  case QMetaType::QRegExp:
773  stream << *static_cast<const NS(QRegExp)*>(data);
774  break;
775 #endif
776 #ifndef QT_BOOTSTRAPPED
778  stream << *static_cast<const NS(QEasingCurve)*>(data);
779  break;
780 #endif
781 #ifdef QT3_SUPPORT
782  case QMetaType::QColorGroup:
783 #endif
784  case QMetaType::QFont:
785  case QMetaType::QPixmap:
786  case QMetaType::QBrush:
787  case QMetaType::QColor:
788  case QMetaType::QPalette:
789  case QMetaType::QIcon:
790  case QMetaType::QImage:
791  case QMetaType::QPolygon:
792  case QMetaType::QRegion:
793  case QMetaType::QBitmap:
794  case QMetaType::QCursor:
797  case QMetaType::QPen:
800  case QMetaType::QMatrix:
807  if (!qMetaTypeGuiHelper)
808  return false;
810  break;
811  default: {
812  const QVector<QCustomTypeInfo> * const ct = customTypes();
813  if (!ct)
814  return false;
815 
816  SaveOperator saveOp = 0;
817  {
818  QReadLocker locker(customTypesLock());
819  saveOp = ct->at(type - User).saveOp;
820  }
821 
822  if (!saveOp)
823  return false;
824  saveOp(stream, data);
825  break; }
826  }
827 
828  return true;
829 }
const QStringList & customTypes
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
Q_CORE_EXPORT const QMetaTypeGuiHelper * qMetaTypeGuiHelper
Definition: qmetatype.cpp:346
static const char * data(const QByteArray &arr)
static int type(const char *typeName)
Returns a handle to the type called typeName, or 0 if there is no such type.
Definition: qmetatype.cpp:607
The QReadLocker class is a convenience class that simplifies locking and unlocking read-write locks f...
void(* SaveOperator)(QDataStream &, const void *)
Definition: qmetatype.h:108
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
QMetaType::SaveOperator saveOp
Definition: qmetatype.cpp:342
QMetaType::SaveOperator saveOp
Definition: qmetatype.cpp:361
static bool isRegistered(int type)
Returns true if the datatype with ID type is registered; otherwise returns false. ...
Definition: qmetatype.cpp:590

◆ type()

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

Returns a handle to the type called typeName, or 0 if there is no such type.

See also
isRegistered(), typeName(), Type

Definition at line 607 of file qmetatype.cpp.

Referenced by MetaObjectGenerator::addProperty(), QAxBase::asVariant(), QDeclarativeObjectMethodScriptClass::callOverloaded(), QDeclarativeObjectMethodScriptClass::callPrecise(), QScript::callQtMethod(), QScriptEnginePrivate::convertValue(), QScript::QObjectConnectionManager::execute(), QDBusMetaObjectGenerator::findType(), QStateMachinePrivate::handleTransitionSignal(), QSignalSpy::initArgs(), QAxBase::internalInvoke(), QMetaMethod::invoke(), QAxServerBase::Invoke(), QScriptEnginePrivate::jscValueToVariant(), QDeclarativePropertyCache::Data::load(), QVariant::load(), QVariant::nameToType(), QScriptEnginePrivate::newQObject(), QDeclarativeDomDynamicProperty::propertyType(), qDBusNameToTypeId(), QDeclarativeBoundSignalParameters::QDeclarativeBoundSignalParameters(), QTest::qSignalDumperCallback(), QAxServerBase::qt_metacall(), QAxBase::querySubObject(), queuedConnectionTypes(), qvariant_nameToType(), QVariantToVARIANT(), QMetaProperty::read(), QMetaProperty::type(), QScript::QScriptMetaType::typeId(), QMetaProperty::userType(), QScript::variantFromValue(), VARIANTToQVariant(), QMetaProperty::write(), and QDeclarativePropertyPrivate::writeEnumProperty().

608 {
609  int length = qstrlen(typeName);
610  if (!length)
611  return 0;
612  int type = qMetaTypeStaticType(typeName, length);
613  if (!type) {
614  QReadLocker locker(customTypesLock());
615  type = qMetaTypeCustomType_unlocked(typeName, length);
616 #ifndef QT_NO_QOBJECT
617  if (!type) {
618  const NS(QByteArray) normalizedTypeName = QMetaObject::normalizedType(typeName);
619  type = qMetaTypeStaticType(normalizedTypeName.constData(),
620  normalizedTypeName.size());
621  if (!type) {
622  type = qMetaTypeCustomType_unlocked(normalizedTypeName.constData(),
623  normalizedTypeName.size());
624  }
625  }
626 #endif
627  }
628  return type;
629 }
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static int qMetaTypeStaticType(const char *typeName, int length)
Similar to QMetaType::type(), but only looks in the static set of types.
Definition: qmetatype.cpp:433
static int type(const char *typeName)
Returns a handle to the type called typeName, or 0 if there is no such type.
Definition: qmetatype.cpp:607
The QReadLocker class is a convenience class that simplifies locking and unlocking read-write locks f...
static int qMetaTypeCustomType_unlocked(const char *typeName, int length)
Similar to QMetaType::type(), but only looks in the custom set of types, and doesn&#39;t lock the mutex...
Definition: qmetatype.cpp:450
uint qstrlen(const char *str)
Definition: qbytearray.h:79
static const char * typeName(int type)
Returns the type name associated with the given type, or 0 if no matching type was found...
Definition: qmetatype.cpp:406
static QByteArray normalizedType(const char *type)
Normalizes a type.
#define NS(x)
Definition: qmetatype.cpp:72

◆ typeName()

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

Returns the type name associated with the given type, or 0 if no matching type was found.

The returned pointer must not be deleted.

See also
type(), isRegistered(), Type

Definition at line 406 of file qmetatype.cpp.

Referenced by QTestData::append(), compare(), convert(), QScriptEnginePrivate::convertValue(), QScriptEnginePrivate::create(), QDBusConnectionPrivate::deliverCall(), QTest::fetchData(), QScript::QScriptMetaType::name(), QVariant::save(), QDBusPendingCallPrivate::setMetaTypes(), QDeclarativeObjectScriptClass::setProperty(), QVariant::typeToName(), and QDeclarativeBinding::update().

407 {
408  enum { GuiTypeCount = LastGuiType - FirstGuiType };
409 
410  if (type >= 0 && type <= LastCoreType) {
411  return types[type].typeName;
412  } else if (type >= FirstGuiType && type <= LastGuiType) {
413  return types[type - FirstGuiType + LastCoreType + 1].typeName;
414  } else if (type >= FirstCoreExtType && type <= LastCoreExtType) {
415  return types[type - FirstCoreExtType + GuiTypeCount + LastCoreType + 2].typeName;
416  } else if (type >= User) {
417  const QVector<QCustomTypeInfo> * const ct = customTypes();
418  QReadLocker locker(customTypesLock());
419  return ct && ct->count() > type - User && !ct->at(type - User).typeName.isEmpty()
420  ? ct->at(type - User).typeName.constData()
421  : static_cast<const char *>(0);
422  }
423 
424  return 0;
425 }
const QStringList & customTypes
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
QByteArray typeName
Definition: qmetatype.cpp:357
static int type(const char *typeName)
Returns a handle to the type called typeName, or 0 if there is no such type.
Definition: qmetatype.cpp:607
The QReadLocker class is a convenience class that simplifies locking and unlocking read-write locks f...
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
static const struct @32 types[]
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421

◆ unregisterType()

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

Unregisters a user type, with typeName.

Since
4.4
See also
type(), typeName()

Definition at line 561 of file qmetatype.cpp.

562 {
564  if (!ct || !typeName)
565  return;
566 
567 #ifdef QT_NO_QOBJECT
568  NS(QByteArray) normalizedTypeName = typeName;
569 #else
570  NS(QByteArray) normalizedTypeName = QMetaObject::normalizedType(typeName);
571 #endif
572  QWriteLocker locker(customTypesLock());
573  for (int v = 0; v < ct->count(); ++v) {
574  if (ct->at(v).typeName == typeName) {
575  QCustomTypeInfo &inf = (*ct)[v];
576  inf.typeName.clear();
577  inf.constr = 0;
578  inf.destr = 0;
579  inf.alias = -1;
580  }
581  }
582 }
const QStringList & customTypes
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
QByteArray typeName
Definition: qmetatype.cpp:357
QMetaType::Destructor destr
Definition: qmetatype.cpp:359
The QWriteLocker class is a convenience class that simplifies locking and unlocking read-write locks ...
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
static const char * typeName(int type)
Returns the type name associated with the given type, or 0 if no matching type was found...
Definition: qmetatype.cpp:406
static QByteArray normalizedType(const char *type)
Normalizes a type.
#define NS(x)
Definition: qmetatype.cpp:72
void clear()
Clears the contents of the byte array and makes it empty.
QMetaType::Constructor constr
Definition: qmetatype.cpp:358

Friends and Related Functions

◆ qMetaTypeId()

int qMetaTypeId ( )
related

Returns the meta type id of type T at compile time.

Note
This class or function is threadsafe.
Since
4.1

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 224 of file qmetatype.h.

229 {
231 }
static int qt_metatype_id()
Definition: qmetatype.h:169

◆ qRegisterMetaType() [1/2]

int qRegisterMetaType ( const char *  typeName)
related
Note
This class or function is 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.

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:

qRegisterMetaType<MyClass>("MyClass");

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

typedef QString CustomString;
qRegisterMetaType<CustomString>("CustomString");
See also
qRegisterMetaTypeStreamOperators(), QMetaType::isRegistered(), Q_DECLARE_METATYPE()

Definition at line 185 of file qmetatype.h.

190 {
191  const int typedefOf = dummy ? -1 : QtPrivate::QMetaTypeIdHelper<T>::qt_metatype_id();
192  if (typedefOf != -1)
193  return QMetaType::registerTypedef(typeName, typedefOf);
194 
195  typedef void*(*ConstructPtr)(const T*);
196  ConstructPtr cptr = qMetaTypeConstructHelper<T>;
197  typedef void(*DeletePtr)(T*);
198  DeletePtr dptr = qMetaTypeDeleteHelper<T>;
199 
200  return QMetaType::registerType(typeName, reinterpret_cast<QMetaType::Destructor>(dptr),
201  reinterpret_cast<QMetaType::Constructor>(cptr));
202 }
static int registerTypedef(const char *typeName, int aliasId)
Definition: qmetatype.cpp:515
static const char * typeName(int type)
Returns the type name associated with the given type, or 0 if no matching type was found...
Definition: qmetatype.cpp:406
static int registerType(const char *typeName, Destructor destructor, Constructor constructor)
Registers a user type for marshalling, with typeName, a destructor, and a constructor.
Definition: qmetatype.cpp:477

◆ qRegisterMetaType() [2/2]

int qRegisterMetaType ( )
related

Call this function to register the type T.

Note
This class or function is threadsafe.
Since
4.2

T must be declared with Q_DECLARE_METATYPE(). Returns the meta type Id.

Example:

int id = qRegisterMetaType<MyStruct>();

To use the type T in QVariant, using 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 with the 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() function.

See also
Q_DECLARE_METATYPE()

Definition at line 234 of file qmetatype.h.

Referenced by convertTypes(), qAxFactory(), qmlRegisterCustomType(), qmlRegisterExtendedType(), qmlRegisterInterface(), qmlRegisterRevision(), qmlRegisterType(), and qmlRegisterUncreatableType().

239 {
240 #ifdef Q_CC_SUN
241  return qMetaTypeId(static_cast<T *>(0));
242 #else
243  return qMetaTypeId(dummy);
244 #endif
245 }
int qMetaTypeId()
Returns the meta type id of type T at compile time.
Definition: qmetatype.h:224

◆ qRegisterMetaTypeStreamOperators()

int qRegisterMetaTypeStreamOperators ( const char *  typeName)
related
Note
This class or function is threadsafe.

Registers the stream operators for the type T called typeName.

Afterward, the type can be streamed using QMetaType::load() and 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);
See also
qRegisterMetaType(), QMetaType::isRegistered(), Q_DECLARE_METATYPE()

Definition at line 206 of file qmetatype.h.

211 {
212  typedef void(*SavePtr)(QDataStream &, const T *);
213  typedef void(*LoadPtr)(QDataStream &, T *);
214  SavePtr sptr = qMetaTypeSaveHelper<T>;
215  LoadPtr lptr = qMetaTypeLoadHelper<T>;
216 
217  qRegisterMetaType<T>(typeName);
218  QMetaType::registerStreamOperators(typeName, reinterpret_cast<QMetaType::SaveOperator>(sptr),
219  reinterpret_cast<QMetaType::LoadOperator>(lptr));
220 }
static void registerStreamOperators(const char *typeName, SaveOperator saveOp, LoadOperator loadOp)
Definition: qmetatype.cpp:374
static const char * typeName(int type)
Returns the type name associated with the given type, or 0 if no matching type was found...
Definition: qmetatype.cpp:406
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71

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