QVariant and std :: size_t

QVariant does not support std :: size_t. What is the correct way to create a QVariant object using std :: size_t value without losing platform specific size restrictions?

+3


source to share


1 answer


QVariant does not support size_t directly, but you can still save it:

QVariant v;
size_t s1 = 5;
v.setValue(s1);
qDebug() << v;

// get back the data
size_t s2 = v.value<size_t>();
qDebug() << s2;

      



If you want to store size_t in a file or database in a consistent way, you can convert it to quint64, which is always 8 bytes. Or quint32 if the largest_size of your platforms is 4 bytes:

QVariant v;
size_t s1 = 5;
quint64 biggest = s1;
qDebug() << "sizeof(quint64) =" << sizeof(quint64);

v.setValue(biggest);
qDebug() << v;

// get back the data
quint64 biggest2 = v.value<quint64>();
qDebug() << biggest2;
size_t s2 = biggest2;

      

+4


source







All Articles