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:
#include "zarzadcaodczytu.h"
#include <QMetaProperty>
#include <QFile>
#include <QMetaType>
#include <qdebug.h>
#include "../editorwindow.h"
#include "ui_editorwindow.h"
ZarzadcaOdczytu::ZarzadcaOdczytu()
{
}
bool ZarzadcaOdczytu
::wczytaj(QString nazwaPliku, EditorWindow
*edytor
) {
//HBMalyMis and HBDuzyMis inherits from HoneyBearsMapItem
qRegisterMetaType<HBMalyMis>("HBMalyMis");
qRegisterMetaType<HBDuzyMis>("HBDuzyMis");
QList< HoneyBearsMapItem * > items;
//in debugger i can see that every objects exists and values are set properly
in >> items;
file.close();
delete edytor->ui->graphicsView->scene();
//PlanszaScene inherits from QGraphicsScene
PlanszaScene * nowa = new PlanszaScene(1336, 640, edytor);
edytor->ui->graphicsView->setScene(nowa);
qDebug() << items.count();
for(HoneyBearsMapItem *item : items){
nowa->addItem(item); // <- in this line app crash, when I try to call metaObject() app crash too
}
return true;
}
HoneyBearsMapItem *ZarzadcaOdczytu::create(const char *classname)
{
if(name == "HBDuzyMis")
return new HBDuzyMis();
if(name == "HBMalyMis")
return new HBMalyMis();
return NULL;
}
{
char * className;
in >> className;
item = ZarzadcaOdczytu().create(className);
int count = meta->propertyCount();
for (int i=0; i<count; ++i) {
if (metaproperty.isWritable()){
const char *name = metaproperty.name();
in >> value;
item->setProperty(name, value);
//every values are read properly
qDebug() << "Property " << name << " " << value;
}
}
return in;
}
#include "zarzadcaodczytu.h"
#include <QMetaProperty>
#include <QFile>
#include <QMetaType>
#include <qdebug.h>
#include "../editorwindow.h"
#include "ui_editorwindow.h"
ZarzadcaOdczytu::ZarzadcaOdczytu()
{
}
bool ZarzadcaOdczytu::wczytaj(QString nazwaPliku, EditorWindow *edytor)
{
//HBMalyMis and HBDuzyMis inherits from HoneyBearsMapItem
qRegisterMetaType<HBMalyMis>("HBMalyMis");
qRegisterMetaType<HBDuzyMis>("HBDuzyMis");
QFile file(nazwaPliku);
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
QList< HoneyBearsMapItem * > items;
//in debugger i can see that every objects exists and values are set properly
in >> items;
file.close();
delete edytor->ui->graphicsView->scene();
//PlanszaScene inherits from QGraphicsScene
PlanszaScene * nowa = new PlanszaScene(1336, 640, edytor);
edytor->ui->graphicsView->setScene(nowa);
qDebug() << items.count();
for(HoneyBearsMapItem *item : items){
nowa->addItem(item); // <- in this line app crash, when I try to call metaObject() app crash too
}
return true;
}
HoneyBearsMapItem *ZarzadcaOdczytu::create(const char *classname)
{
QString name(classname);
if(name == "HBDuzyMis")
return new HBDuzyMis();
if(name == "HBMalyMis")
return new HBMalyMis();
return NULL;
}
QDataStream &operator>>(QDataStream &in, HoneyBearsMapItem *item)
{
char * className;
in >> className;
item = ZarzadcaOdczytu().create(className);
const QMetaObject *meta = item->metaObject();
int count = meta->propertyCount();
for (int i=0; i<count; ++i) {
QMetaProperty metaproperty = meta->property(i);
if (metaproperty.isWritable()){
const char *name = metaproperty.name();
QVariant value;
in >> value;
item->setProperty(name, value);
//every values are read properly
qDebug() << "Property " << name << " " << value;
}
}
return in;
}
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 :/
Bookmarks