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

Alan Turing picture Alan Turing · May 30, 2011 · Viewed 8k times · Source

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.

Answer

Raiv picture Raiv · May 30, 2011

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.