4

QVariant does not support std::size_t. What's the proper way to construct a QVariant object using a std::size_t value without losing any platform dependent size restrictions?

Baradé
  • 1,290
  • 1
  • 15
  • 35

1 Answers1

5

QVariant does not support size_t directly, but you can still store 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_t 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;
fxam
  • 3,874
  • 1
  • 22
  • 32