long-integerqvariant

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.


Solution

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