QVariant Serialization

Use this forum to request new features or suggest modifications to existing features

QVariant Serialization

Postby Kasuax » Mon Feb 06, 2017 9:41 pm

Code: Select all
template <class Archive>
inline void qx_save(Archive & ar, const QVariant & t, const unsigned int file_version)


This function handles all the basic QVariant types but it doesn't include other common QMetaTypes that are a superset of QVariant...
Would be an easy upgrade and then QxOrm will understand how to serialize floats too...
For now I'm going to upgrade my local library I guess.

Thanks!

class Q_CORE_EXPORT QMetaType {
public:
enum Type {
// these are merged with QVariant
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 /* QColorGroup */,
#ifdef QT3_SUPPORT
QColorGroup = 63,
#endif
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 */,
VoidStar = 128, Long = 129, Short = 130, Char = 131, ULong = 132,
UShort = 133, UChar = 134, Float = 135
, QObjectStar = 136, QWidgetStar = 137,
QVariant = 138,
LastCoreExtType = QVariant,

// This logic must match the one in qglobal.h
#if defined(QT_COORD_TYPE)
QReal = 0,
#elif defined(QT_NO_FPU) || defined(QT_ARCH_ARM) || defined(QT_ARCH_WINDOWSCE) || defined(QT_ARCH_SYMBIAN)
QReal = Float,
#else
QReal = Double,
#endif

User = 256
};
Kasuax
 
Posts: 62
Joined: Mon Jun 20, 2016 6:42 pm

Re: QVariant Serialization

Postby qxorm » Fri Feb 24, 2017 9:51 am

Hello,

OK, I will add all following QMetaType types in the next version of QxOrm library :
Long = 129, Short = 130, Char = 131, ULong = 132, UShort = 133, UChar = 134, Float = 135


But with current version of QxOrm library, I think there is also a solution to serialize your floats (or other not default QVariant types).
In the Qt QVariant documentation, it is written (http://doc.qt.io/qt-5/qvariant.html#type) :
Also note that the types void*, long, short, unsigned long, unsigned short, unsigned char, float, QObject*, and QWidget* are represented in QMetaType::Type but not in QVariant::Type, and they can be returned by this function. However, they are considered to be user defined types when tested against QVariant::Type.


And in QxOrm library, this is possible to serialize a QVariant user type, if type is not found in the serialization function, following code is called :
Code: Select all
qx::QxClassX::type_fct_save_qvariant_usertype fct;
fct = qx::QxClassX::getFctSaveQVariantUserType();
if (! fct.empty()) { sUserType = fct(t, iType, file_version); }
ar << boost::serialization::make_nvp(sTag, sUserType);


That means that qx::QxClassX singleton class can hold your own function to serialize QVariant user types.
So you must define and implement :
- a save function with this signature : QString myCustomSaveQVariant(const QVariant & val, int type, const unsigned int file_version)
- a load function with this signature : QVariant myCustomLoadQVariant(const QString & val, int type, const unsigned int file_version)
Then, just register your 2 save/load functions like this :
Code: Select all
qx::QxClassX::setFctSaveQVariantUserType(& myCustomSaveQVariant);
qx::QxClassX::setFctLoadQVariantUserType(& myCustomLoadQVariant);

Then, these functions will be called automatically by QxOrm library saving/loading your QVariant specific types...
qxorm
Site Admin
 
Posts: 476
Joined: Mon Apr 12, 2010 7:45 am


Return to QxOrm - Feature request

Who is online

Users browsing this forum: No registered users and 1 guest

cron