Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QVariant and std::size_t

Tags:

c++

qt

qvariant

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?

like image 746
Baradé Avatar asked Oct 25 '25 10:10

Baradé


1 Answers

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;
like image 79
fxam Avatar answered Oct 26 '25 23:10

fxam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!