How to convert an unsigned long int to QVariant
If I want to store a long
in a QVariant
, I am obligated to convert first the value to long long
.
QVariant store (unsigned long int input) {
unsigned long long data = (unsigned long long) input;
QVariant qvariant( data );
return qvariant;
}
unsigned long int load (const QVariant& qvariant) {
bool ok;
unsigned long int data = (unsigned long) qvariant.toULongLong(&ok);
if (ok)
return data;
else
return NAN;
}
This problem don't concern the design QVariant class. but it's the problem of long type.
The long type change but int (4) or long long (8) is the same in all LLP64/IL32P64 LP64/I32LP64 as wikipedia note.
Intel Developer zone say :
Suggestion: If it is important to you for integer types to have the same size on all Intel platforms, then consider replacing "long" by either "int" or "long long". The size of the "int" integer type is 4 bytes and the size of the "long long" integer type is 8 bytes for all the above combinations of operating system and architecture.
Good luck
/Mohamed
It's very likely (according to the question title) that topic starter has received the following error message from the compiler:
error: conversion from ‘uint64_t {aka long unsigned int}’ to ‘QVariant’ is ambiguous
None of the answers suggested provides a simple solution. So, instead of implicit conversion from a value, something like
QVariant_value = long_unsigned_int_value;
try the following:
QVariant_value = QVariant::fromValue(long_unsigned_int_value)
This helped me.