Serialize Deserialize

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):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

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 = new struct 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;
   } ;


Thanks in advance!
Last edited on
Two things to try:

In zeichenFeld::serialize():
1
2
3
4
5
6
    << (*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);
      }
Thanks, that brought me a step further!
Topic archived. No new replies allowed.