0

So far in my project where I had to store different data types in one place and in the end send them through a medium bytewise, I always used a custom union:

union union64_t
{
    quint16 u16[4];
    qint16  i16[4];

    quint32 u32[2];
    qint32  i32[2];
    float   f32[2];

    quint64 u64;
    qint64  i64;
    double  f64;

};

Is there a way to use Qvariant instead of this union? Even If I assign a q variant with something, ie:

Qvariant test(1.25f);

How can I extract it as bytes (assuming it was a double or float type)? I would appreciate all help.

timrau
  • 22,578
  • 4
  • 51
  • 64
Łukasz Przeniosło
  • 2,725
  • 5
  • 38
  • 74
  • Note: Type punning is supported in C++ (it is supported in C BTW). – Jarod42 Jan 27 '17 at 12:03
  • In all cases, sending a `quint64` *bytewise* over the network is not the best idea since the [endiannes](https://en.wikipedia.org/wiki/Endianness) might differ between hosts. – Mike Jan 27 '17 at 12:13
  • I am sending this through CAN (controller area network). All targets are little endian. – Łukasz Przeniosło Jan 27 '17 at 12:21

1 Answers1

0

It doesn't look like QVariant supports what you want to do. The closest method would be toByteArray, but it only returns data if the original stored values are already a QByteArray or a QString.

goug
  • 2,294
  • 1
  • 11
  • 15