13

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.

Antonio
  • 851
  • 2
  • 8
  • 17

3 Answers3

13

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.

Dmitry Maksimov
  • 131
  • 1
  • 5
  • 2
    For the search engines: this answer also works in case of error message `error: conversion from ‘long int’ to ‘const QVariant’ is ambiguous`. – tanius Jun 17 '20 at 13:43
2

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;
 }
Antonio
  • 851
  • 2
  • 8
  • 17
  • is this another question or a refinement of your actual question?? because here is the section where the answere are being proposed. – isaias-b Jun 17 '14 at 09:23
  • Well, I have this answer, so what I would like to know is if there are any other way to overcome this. I would say that this is a refinement. For example. I could overcome this by converting the data into `QString`, where I have the functionality for convert `ulong`, but I think that would be more complex. Thanks – Antonio Jun 17 '14 at 09:25
  • so please put everything belonging to the question inside of the question, edit the question to make any refinement as required ;) – isaias-b Jun 17 '14 at 09:32
  • 1
    Since the second edition I have not ask any other question nor put additional information in this answer. All the relevant information is inside the question. Why this answer has been vote to deletion? What is wrong here? – Antonio Jun 17 '14 at 11:00
1

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

Mohamed Hamzaoui
  • 325
  • 4
  • 15
  • As far as I know, `long int` are usually 4 bytes long, but `int` size may vary depending of the platforms (it can be stored in less than 4bytes). However, more of the current desktop platforms implement `int` using 4 bytes (minimum word size; 32b). Therefore, I am more happy using `long` which by standard definition allows a larger number of integers. unsigned long: 0 to 4294967295. unsigned int: 0 to 65535. Thank you Mohamed – Antonio Jun 17 '14 at 10:33
  • I think your first remark is wrong Look at the link I put in the post especially [here](https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models). – Mohamed Hamzaoui Jun 17 '14 at 12:29
  • Sorry, but I do not see your point. Your link shows different sizes for both, `long int` and `int`. In 64bit-word length architectures is reasonable that they use a full word to represent a variable for most of types. However, shorter world-length architectures may use smaller size. The size depends ultimately of the platform. Said so, as far as I know. A platform can implement `int` of 2 bytes long and still being standard C/C++ (0-65535). Not all the architectures are 32b or 64b. – Antonio Jun 17 '14 at 13:52