1

I want to save a QList<QList<MyClass>> as QVariant but I alway get the error QVariant::save: unable to save type 'QList<QList<MyClass*> >' (type id: 1042). I've implemente the streaming operator for MyClass and it works fine.

QDataStream& operator<<(QDataStream& out, const MyClass& item)
{   
    //Properties to write
    return out;
}

QDataStream& operator>>(QDataStream& in, MyClass& item)
{   
    //Properties to read
    return in;
}

I also registered the types

Q_DECLARE_METATYPE(MyClass*)
Q_DECLARE_METATYPE(QList<MyClass*>)
Q_DECLARE_METATYPE(QList<QList<MyClass*>>)

and

qRegisterMetaType<QList<MyClass*>>("QList<MyClass*>");
qRegisterMetaType<QList<QList<MyClass*>>>("QList<QList<MyClass*>>");
qRegisterMetaType<MyClass*>("MyClass*");

If I only stream one MyClass Object everything is fine. I can save it as QVariant, stream it and convert it back to MyClass. I thought I don't have to implement a operator for QList<MyClass> or QList<QList<MyClass>> because streaming a Template Class in a QList is already implemented by Qt. So whats the matter?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
RobRobRob
  • 67
  • 2
  • 10
  • MyClass inherits from QObject or from some kind of Qt? – eyllanesc Feb 08 '18 at 14:51
  • 2
    A `MyClass*` and a `MyClass` are not the same thing. Are you sure you want to store a `MyClass*` in your list of lists? – NathanOliver Feb 08 '18 at 14:52
  • I pass the MyClass pointer to the streaming operator and only stream things like QString, QPointF, etc.. So I don't stream the pointer itself just copy the properties and set them to a new created pointer object. @eyllanesc Yep, it inherites from QObject. And as I said for single objects its no problem but for the Objects in a QList or QList of QLists. – RobRobRob Feb 08 '18 at 14:57
  • @NathanOliver - internally QList will use pointers nonetheless. Which is how it even works for QObject which is not copyable. – dtech Feb 08 '18 at 18:30

0 Answers0