开发者

Is there a reason why QVariant accepts only QList and not QVector nor QLinkedList

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开发者_如何学Python know that it's a vector of objects.


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜