11

I want to know, How QVariant can internally stores, int, QMap, QList,...

I mean what is the internal data-structure/Implementation? What is the overhead of storing and retrieving types (int, float) in QVariant?

SunnyShah
  • 28,934
  • 30
  • 90
  • 137

1 Answers1

19

A quick look at the code reveals that a QVariant is basically a union of several primitive types (int, float etc'), a QObject pointer, and a void* pointer for anything else that is not a QObject and not a primitive. There is also a type data member that allows it to know what's actually currently stored there. The overhead appears to be not much more than storing to a member of a struct, checking that for type compatibility and possibly making a conversion (int to float for instance)

shoosh
  • 76,898
  • 55
  • 205
  • 325
  • 2
    As additional overhead, it also needs to store what the underlying type is. This makes `QVariant` a total of _16 bytes_: 8 bytes (largest primitive type) + 4 bytes (type info) + 4 bytes (padding). – hrr Nov 30 '11 at 08:45
  • 2
    At least as of now, it has a pimpl. I think the size is now 32bytes, 16 for the `QVariant` and 16 for the `QVariant::Private`. At least, that is what `sizeof` tells me. I have not counted them by hand. – derM - not here for BOT dreams Sep 22 '17 at 08:01