0

I have a problem saving a QList of QSharedPointer< MyClass > as QVariant in a derived QGraphicsItem class. MyClass is an abstract base class used as an interface. I made this to be able to up cast all 'MyClass subclasses' to the base MyClass and put them all into one container (list, map, etc.).

Console output:

kernel\qvariant.cpp(2150): QVariant::save: unable to save type <'QList < QSharedPointer< MyClass> >' (type id: 1035).

global\qglobal.cpp(3052): ASSERT failure in QVariant::save: "Invalid type to save", file kernel\qvariant.cpp, line 2151

Code abstract:

SampleClass::SampleClass()
{
    qRegisterMetaType<QSharedPointer<MyClass> >("QSharedPointer<MyClass>");
    qRegisterMetaType<QList<QSharedPointer<MyClass>>>("QList<QSharedPointer<MyClass>>");

    QList<QSharedPointer<MyClass> > myClassList;
    myClassList.append(...); // Just to make clear that the list is filled with QSharedPointer<MyClass>
    QVariant varMyClass;
    varMyClass.setValue(myClassList); //QVariant::fromValue(myClassList)

    DerivedQGraphicsItem *newCustomItem = new DerivedQGraphicsItem;
    newCustomItem->setData(Qt::UserRole, varMyClass);
}

This works fine but when i add newCustomItem to a QGraphicsScene and click it the error above occures. It seems that the stream operators have to be implemented (QDataStream &operator<< and QDataStream &operator>>) for QSharedPointer< MyClass >. But is it possible and does this make sense?

Community
  • 1
  • 1
RobRobRob
  • 67
  • 2
  • 10

0 Answers0