Hi,
I try to serialize my own class, which inherits from QGraphicsItem and QObject, but i have a problem when i want to read it from file:

Qt Code:
  1. #include "zarzadcaodczytu.h"
  2.  
  3. #include <QMetaProperty>
  4. #include <QFile>
  5. #include <QMetaType>
  6. #include <qdebug.h>
  7.  
  8. #include "../editorwindow.h"
  9. #include "ui_editorwindow.h"
  10.  
  11.  
  12.  
  13. ZarzadcaOdczytu::ZarzadcaOdczytu()
  14. {
  15. }
  16.  
  17. bool ZarzadcaOdczytu::wczytaj(QString nazwaPliku, EditorWindow *edytor)
  18. {
  19. //HBMalyMis and HBDuzyMis inherits from HoneyBearsMapItem
  20. qRegisterMetaType<HBMalyMis>("HBMalyMis");
  21. qRegisterMetaType<HBDuzyMis>("HBDuzyMis");
  22.  
  23. QFile file(nazwaPliku);
  24. file.open(QIODevice::ReadOnly);
  25. QDataStream in(&file);
  26. QList< HoneyBearsMapItem * > items;
  27.  
  28. //in debugger i can see that every objects exists and values are set properly
  29. in >> items;
  30. file.close();
  31.  
  32. delete edytor->ui->graphicsView->scene();
  33.  
  34. //PlanszaScene inherits from QGraphicsScene
  35. PlanszaScene * nowa = new PlanszaScene(1336, 640, edytor);
  36. edytor->ui->graphicsView->setScene(nowa);
  37.  
  38.  
  39. qDebug() << items.count();
  40.  
  41. for(HoneyBearsMapItem *item : items){
  42. nowa->addItem(item); // <- in this line app crash, when I try to call metaObject() app crash too
  43. }
  44.  
  45.  
  46. return true;
  47. }
  48.  
  49. HoneyBearsMapItem *ZarzadcaOdczytu::create(const char *classname)
  50. {
  51. QString name(classname);
  52.  
  53. if(name == "HBDuzyMis")
  54. return new HBDuzyMis();
  55. if(name == "HBMalyMis")
  56. return new HBMalyMis();
  57.  
  58. return NULL;
  59. }
  60.  
  61. QDataStream &operator>>(QDataStream &in, HoneyBearsMapItem *item)
  62. {
  63. char * className;
  64. in >> className;
  65.  
  66. item = ZarzadcaOdczytu().create(className);
  67.  
  68. const QMetaObject *meta = item->metaObject();
  69. int count = meta->propertyCount();
  70. for (int i=0; i<count; ++i) {
  71. QMetaProperty metaproperty = meta->property(i);
  72. if (metaproperty.isWritable()){
  73. const char *name = metaproperty.name();
  74. QVariant value;
  75. in >> value;
  76. item->setProperty(name, value);
  77.  
  78. //every values are read properly
  79. qDebug() << "Property " << name << " " << value;
  80.  
  81. }
  82.  
  83. }
  84.  
  85.  
  86. return in;
  87.  
  88. }
To copy to clipboard, switch view to plain text mode 

What I'm doing wrong :/ ? In the operator >> function I can call metaObject(), but when I try do this outside this function after it, the application crash. Other objects methods works fine. The main problem is when I try add object to scene :/