Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 know that it's a vector of objects.

like image 908
Alan Turing Avatar asked May 30 '11 15:05

Alan Turing


1 Answers

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.

like image 141
Raiv Avatar answered Sep 23 '22 05:09

Raiv