Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an unsigned long int to QVariant

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.

like image 540
Antonio Avatar asked Jun 17 '14 09:06

Antonio


People also ask

Can unsigned long be zero?

The minimum value that can be stored in unsigned long long int is zero.

What is QVariant in Qt?

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.

How do you declare unsigned long in C++?

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; }


2 Answers

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.

like image 54
Dmitry Maksimov Avatar answered Oct 20 '22 11:10

Dmitry Maksimov


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;
 }
like image 30
Antonio Avatar answered Oct 20 '22 11:10

Antonio