I have realized that QVariant does not offer functionality for long
and unsigned long
. It offers conversions to int
, unsigned int
, long long
and unsigned long long
.
We can find in current Desktop architectures that long
and int
are equivalent, but they are not from a theoretical point of view.
If I want to store a long
in a QVariant
I am obligated to convert first the value to long long
. I would like to know if there is any other way to overcome this.
Secondly, I am interested to know the better/simpler way to do it. I.e. using a simpler code, and avoiding the use of unnecessary space or instructions.
The minimum value that can be stored in unsigned long long int is zero.
Because C++ forbids unions from including types that have non-default constructors or destructors, most interesting Qt classes cannot be used in unions. Without QVariant, this would be a problem for QObject::property() and for database work, etc. A QVariant object holds a single value of a single typeId() at a time.
include #include #define ull unsigned long long #define ll long long using namespace std; int main() { ll a, b, m; cin >> a >> b >> m; ll q = (long double)a * b / m; ll res = a * b — q * m; while(res < 0) { res += m; } while(res >= m) { res -= m; } cout << res; }
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With