I'm working with Qt and C++. I have to manage error codes of an electronic board. I can get these errors in the form of QByteArray that I have to convert into a QVariant. I did not find any practial method to do this (so if you have one I'm here listening).Anyway, trying to convert it (the QByteArray) to the QVariant, the only method I found is to use a code like this:
QByteArray value; //This is the QByteArray where I have the error code
QVariant qVariantOut; //This is the QVariant where I want to put the error
qVariantOut = QVariant(*(quint64*)(void*)(&(value).data()[0]));
Because of the bad casting steps I used, I have stumbled upon the various good casting rules and I did somethig like this:
qVariantOut = QVariant(*(static_cast<quint64*(static_cast<void*>((&(value).data()[0]))));
These casts seem to work and so I decided to deepen the casting subject but I don't understand some results I got. Following I present all the cases I have tried. If someone could give me an explanation (I'll present mine) of what is happening in each case it would be great.
qVariantOut = QVariant(*((quint64 *)value.data()));
I think this works because value.data() returns a char* and the cast quint64* do a reinterpret_cast (if I interpreted well what it is said here: When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used? ). Is this reason correct?qVariantOut = QVariant(*(static_cast<quint64*>(value.data()) ));
This one does't work because I'm trying to do a static_cast directly to an quint64*. But here I have one question: why it is not possible to cast from char* to quint64*. Aren't them some basics (POD) type for which a static_cast have to be possible?
I would like to understand better these casts... I even find someone that said: "if you need to cast pointers, cast them via void*". Comments?
qVariantOut = QVariant( (quint64*)(value.data()) );
This is something like a bonus. I'm trying with this code to put in a QVariant a pointer... This give me the error "QVariant::QVariant(void) is private within this context*" and I don't get what this means.
Thank you.
EDIT: For the users that said it could be an XY problem. Here are some more information. I wrote a function to get variables from an electronic board. These variables could be QString, quint64, qint32, quint8 and so on...In my function, in order to get these variables, I use an external function (coming from an external library, developed internally by the electronic division of my company, I have to use it but can't modify it). In order to use this external function I need to pass as parameters: the variable I want to read (i.e.: errors, warnings, temperatures, version of firmware...), an output QByteArray and the size of the variable I want to read(for example errors->sizeof(quint64), temperatures->sizeof(quint8)). This external function, as you understand, returns a QByteArray.
The fact that I present the code with a cast to quint64 is only a case of my function.
I want to convert this QByteArray to a QVariant so my function can return this qVariantOut that I will convert to the correct variable (for example: I know that I need to read the error variable of the board, I call my function that will set the variable size and pass it to the external function (together with the variable name) that will return a QByteArray. This QByteArray will be converted in a QVariant returned outside my function. Outside I will convert the QVariant to the right variable for example a quint64 for the errors, using QVariant methods). Note that I do this because of the constraint of the system (I need to use that external function that returns always a QByteArray for every type of variable I want to read) and because I did not find a better and more practical method to convert a QByteArray to the final variable (ex.: if I use qba.toLongLong
, with qba a QByteArray, it doesn't work)... if you have one, as I said before, I'm here listening to you.
Anyway I don't want to focus too much on the XY problem (but if it is an XY problem I want to resolve it obviously) but I want to understand better that casting rules and to have a constructive discussion on my doubt and questions :)