Store the data as a member variable d of a private struct type:
<qvariant.cpp>
1 qvariant::qvariant (Type type) 2 {Create (type, 0);}
1 void qvariant::create (int type, const void *copy) 2 {3 d.type = type;4 handler->construct (&d, copy); 5}
static void Construct (qvariant::P rivate *x, const void *copy) { x->is_shared = false; Switch (x->type) {case qvariant::string: v_construct<qstring> (x, copy); break; ...... Default: void *ptr = Qmetatype::construct (x->type, copy); if (!ptr) { x->type = qvariant::invalid; } else { x->is_shared = true; x->data.shared = new qvariant::P rivateshared (PTR); } break; } X->is_null =!copy;}
1 qvariant::qvariant (int val) 2 {d.is_null = false; d.type = int; d.data.i = val;}
<qvariant.h>
Class Q_core_export qvariant{... struct private { inline private (): Type (Invalid), is_shared (false), Is_null (true) {data.ptr = 0;} Inline private (const private &other) : Data (Other.data), type (other.type), is_shared (other.is_shared), Is_null (Other.is_null) {} Union Data { char C; int i; UINT U; BOOL B; Double D; float F; Qreal Real; Qlonglong ll; Qulonglong ull; Qobject *o; void *ptr; Privateshared *shared; } data; UINT Type:30; UINT Is_shared:1; UINT is_null:1; }; ...... Private D; ......}
Data types supported by Qvariant:
1 enum Type {2 Invalid = 0, 3 4 Bool = 1, 5 Int = 2, 6 UInt = 3, 7 Longlong = 4 , 8 Ulonglong = 5, 9 Double = 6,10 Char = 7,11 Map = 8,12 List = 9,13 stri ng = 10,14 Stringlist = 11,15 ByteArray = 12,16 BitArray = 13,17 Date = 14,18 time = 15,19 DateTime = 16,20 Url = 17,21 Locale = 18,22 Rect = 19,23 RECTF = 20,24 Size = 21,25 SizeF = 22,26 line = 23,27 Linef = 24,28 point = 25,29 PointF = 26,30 REGEXP = 27,31 Hash = 28,32 Easingcurve = 29,33 Lastcoretype = easingcurve,34 35 Value internally reserved36 #ifdef qt3_support37 colorgroup = 63,38 #endif39 Font = 64,40 Pixmap = 65,41 Brush = 66,42 Color = 67,43 Palette = 68,44 Icon = 69,45 Ima GE = 70,46 PolygOn = 71,47 Region = 72,48 Bitmap = 73,49 Cursor = 74,50 Sizepolicy = 75,51 keysequ ence = 76,52 Pen = 77,53 TextLength = 78,54 TextFormat = 79,55 Matrix = 80,56 Tran Sform = 81,57 matrix4x4 = 82,58 vector2d = 83,59 Vector3D = 84,60 vector4d = 85,61 quaternion = 86,62 Lastguitype = quaternion,63 usertype = 127,65 #ifdef qt3_support66 IconSet = icon,67 CString = bytearray,68 Pointarray = polygon,69 #endif70 Lasttype = 0xffffffff//need th is, so, GCC >= 3.4 allocates, bits for Type71};
Data type conversions:
The following data types can be automatically converted
The conversion of the specified data type can be determined by the member function bool Qvariant::canconvert (Type t) const
Custom Qvariant data types that can be stored:
Class Q_core_export qvariant{... Template<typename t> BOOL Canconvert () const {return Canconvert (Type (qmetatypeid<t> ()));} ......}
1 static inline Qvariant fromvalue (const T &value) 2 {return qvariantfromvalue (value);}
Template <typename t>inline qvariant qvariantfromvalue (const T &t) { return qvariant (qmetatypeid<t > (reinterpret_cast<t *> (0)), &t, qtypeinfo<t>::ispointer);}
As you can see from the declaration of a class, to be a data type that qvariant can store, you must register the custom data type with a macro q_declare_metatype (type) into the Metatype system
<qmetatype.h>
1 #define Q_DECLARE_METATYPE (TYPE) 2 qt_begin_namespace 3 Template <> 4 struct qmetatypeid< TYPE > 5 { 6 enum {Defined = 1}; 7 static int qt_metatype_id () 8 { 9 static Qbasicatomicint metatype_id = Q_basic_atomic_initializer (0); if (!metatype_id) metatype_id = Qregisterm etatype< type > (#TYPE, reinterpret_cast< type *> (Quintptr (-1))); return metatype_id; 14} 15}; Qt_end_namespace
Example:
namespace mynamespace{ struct mystruct { int i; ... };} Q_declare_metatype (mynamespace::mystruct)
1 mystruct S; 2 qvariant var; 3 Var.setvalue (s); 4 5 ... 6 7 Qvariant var2 = Qvariant::fromvalue (s); 8 if (var2.canconvert<mystruct> ()) 9 {ten mystruct s2 = V Ar2.value<mystruct> (); 11}
Http://www.cnblogs.com/paullam/p/3706371.html
Qvariant equivalent to a consortium containing most QT data types (source interpretation)