7

QVariant appears to accept QList<QVariant> and not QVector<QVariant> nor QLinkedList<QVariant>. Is it simply because it sees QList, QVector and QLinkedList as fundamentally similar (in an abstract sense) data structures?

I'm adding and std::vector to a QVariant. If using only the Qt API and not a manual conversion, this requires two conversions:

  1. From std::vector to QVector
  2. From QVector to QList

PS: I'm aware that I can add std::vector to QVariant directly with this but I believe in that case it won't know that it's a vector of objects.

2
  • I'm confused about what you are asking. First you discuss lists and vectors of QVariants, then you discuss storing std::vector in a QVariant? Are there two separate questions here?
    – Nathanael
    May 30, 2011 at 15:12
  • The first question is the focus. The second statement simply describes what I'm using it for. May 30, 2011 at 18:05

4 Answers 4

12

you may store everything in QVariant, after calling to qRegisterMetaType function.

so if you call qRegisterMetaType<std::vector<SomeObject> >("std::vector<SomeObject>"); QVariant WOULD store std::vector. reading such values from it performing function T QVariant::value () const , for writing use function void QVariant::setValue ( const T & value )

PS: I'm aware that I can add std::vector to QVariant directly with this but I believe in that case it won't know that it's a vector of objects.

When you registering type to QVariant, it calls it's default constructor,copy constructor and destructor while manipulating items of that type. So there is no harm to use it with classes and objects.

1
  • Yes, but the problem is that I want the QVariant to be recognized as a collection of items by another library that expects Qt types (QJson). I'm using QJson which works with QVariant and wouldn't know what to do with a "user-defined" type such as std::vector. May 30, 2011 at 18:07
7

Simply because QList is by far the most commonly used container type, and adding overloads for all the others would make the QVariant interface even more complex than it already is. In any case, your problem seems to be not that QVariant doesn't support QVector (it does with a little work) but that QJson doesn't. I doubt that an extra call to QVector::toList() would cause a significant performance overhead.

2

For templated classes like these, you have to register each specific instantiation with the meta system individually. Apparently, the Trolls felt the need to use QList<QVariant>, but none of the others, so they only registered the one. There's no specific reason you couldn't register them yourself.

0

I'm not 100% sure of the implementation of QVariant, but I believe that the size of a QVariant is not determined until run time. This means that if you try to write QVector< QVariant > the compiler doesn't know how much space to allocate, so it reports an error. The same is true for LinkedList. QList works because it's implementation relies strictly on pointers.

I bet you would find that QVector< QVariant* > compiles just fine.

Big caveat: I'm not a Qt expert, so I might be off on this. But hopefully at the very least this gets you thinking in the right direction.

1
  • For any type, the size has to be known at compile time. If you don't know the size until runtime, then you have to rely on the heap, in which case the actual structure's size will still be known as the size of a pointer. May 30, 2011 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.