Hallo All,
I'm trying to serialize and deserialize graphic objects, but it doesen't work. What exactly is wrong? Here you can find the code (.cpp and .h):
void zeichenFeld::serialize(QFile &file)
{
vector<struct myDrawingObject *>::iterator pos;
QTextStream out(&file);
for (pos=objects.begin();pos!=objects.end();pos++)
{
out << "p "
<< (*pos)->x << " " << (*pos)->y<< " "
<< (*pos)-> phaseFarbe <<" "<<(*pos)-> phaseKoord <<""
<< (*pos)->koord1 << " "
<< (*pos)->koord2 << " "<< (*pos)->rot <<" "
<< (*pos)->gruen << " " << (*pos)->blau << endl;
if ((*pos)->type == dreieck ) out <<1;
if ((*pos)->type == circle) out <<2;
}
}
void zeichenFeld::deserialize(QFile &file)
{
struct myDrawingObject *object;
char c;
int dreieck, pie, kreis;
QTextStream in(&file);
while (in.status() == QTextStream::Ok)
{
in >> c;
if (in.status() == QTextStream::ReadPastEnd) break;
if (c!='p')
{
QMessageBox::warning(this, tr("Objektfehler"),
tr("Folgender Objekttyp ist unbekannt: ") + c,QMessageBox::Ok);
return;
}
object = newstruct myDrawingObject;
in >> object->x;
in >>object->y;
in >>object->phaseFarbe;
in >>object->phaseKoord;
in >>object->koord1;
in >>object->koord2;
in >>object->rot;
in >>object->gruen;
in >>object->blau;
in >> 1 = dreieck;
in >> 2 = kreis;
in >> c;
objects.push_back(object);
}
update();
}
1 2 3 4 5 6 7 8 9 10 11 12 13
struct myDrawingObject
{
zeichenFeld::drawType type;
int x;
int y;
int phaseKoord;
int phaseFarbe;
int rot;
int gruen;
int blau;
int koord1;
int koord2;
} ;
<< (*pos)->koord2 << " "<< (*pos)->rot <<" "
<< (*pos)->gruen << " " << (*pos)->blau; //<< endl; move this
if ((*pos)->type == dreieck ) out <<1;
if ((*pos)->type == circle) out <<2;
out << endl; // to here
}
In zeichenFeld::deserialize():
1 2 3 4 5 6 7 8 9
in >>object->blau;
// in >> 1 = dreieck; // You're only writing one value in serialize()
// in >> 2 = kreis;
int d_type; // writing an int above
in >> d_type;
object->type = static_cast<drawType>(d_type);
// in >> c; // ????
objects.push_back(object);
}