PDA

View Full Version : QDataStream and saving and loading QList



Noxxik
1st March 2009, 13:47
Hi I am trying to save QList into QDataStream and reading from QDataStream.
I have this code.
writing:

QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
return false;

QDataStream outStream(&file);
outStream.setVersion(QDataStream::Qt_4_4);

outStream << (quint32)0xA0B0C0D0;
outStream << (qint32)123;

outStream << list;
It seems good, but when I try to read file like this.

QFile file(fileName);
if(!file.open(QIODevice::ReadOnly))
return false;

QDataStream inStream(&file);
inStream.setVersion(QDataStream::Qt_4_4);

quint32 magicWord;
inStream >> magicWord;
if (magicWord != 0xA0B0C0D0)
return false;

while(!file.atEnd())
{
inStream >> list;
}list is private QList of my items. I thought that it will be OK according THIS (http://doc.trolltech.com/4.4/qlist.html#operator-gt-gt-29), but it isnot :( Could someone help me please, how can I solve this?

Noxxik
1st March 2009, 21:43
I found in google that this is not good.
So I try this



bool MainScene::loadFile(const QString &fileName)
{
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly))
return false;

QDataStream inStream(&file);
inStream.setVersion(QDataStream::Qt_4_4);

quint32 magicWord, second;
inStream >> magicWord >> second;
if (magicWord != 0xA0B0C0D0) return false;

QApplication::setOverrideCursor(QCursor(Qt::WaitCu rsor)); // set wait cursor during loading

inStream >> list;
QApplication::restoreOverrideCursor();
file.close();
return true;
}

bool MainScene::saveFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
return false;

QApplication::setOverrideCursor(QCursor(Qt::WaitCu rsor)); // set wait cursor during saving

QDataStream outStream(&file);
outStream.setVersion(QDataStream::Qt_4_4);

outStream << (quint32)0xA0B0C0D0;
outStream << (qint32)123;

outStream << list;
QApplication::restoreOverrideCursor();
file.close();
return true;
}


QDataStream &operator<<(QDataStream &out, const Item *item)
{
out << qint16(item->itemType()) << item->sceneBoundingRect() << item->pen() << item->brush() << item->pos() << item->zValue();
return out;
}

QDataStream &operator>>(QDataStream &in, Item *item)
{
qint16 type;
QRectF rect;
QPen pen;
QBrush brush;
QPointF point;
int zValue;

in >> type >> rect >> pen >> brush >> point >> zValue;
item = new Item((Item::ItemType)type, rect);
item->setPen(pen);
item->setBrush(brush);
item->setPos(point);
item->setZValue(zValue);
return in;
}

But programm crash when I try to load my file :(
What is wrong? Or I cannot send to QDataStream all QRect or QPen or QBrush, etc?


edit: or I have to "convert" all QRect or QPen or QBrush, etc... to uint which is described in THIS (http://doc.trolltech.com/4.4/datastreamformat.html)? It could be hard, isnt it?

lni
1st March 2009, 22:00
Your reading doesn't match the writing, change it to:


QFile file(fileName);
if(!file.open(QIODevice::ReadOnly))
return false;

QDataStream inStream(&file);
inStream.setVersion(QDataStream::Qt_4_4);

quint32 magicWord;
inStream >> magicWord;
if (magicWord != 0xA0B0C0D0)
return false;

qint32 id;
inStream >> id; // should be 123

inStream >> list; // read the list saved by outStream >> list;



You can't read to the end, those data belong to other object, just read what you save.

By the way, if you have to check the "magicWord" and if it is false, you should rewind the pointer back so the code continue to work correctly... You can't just read 4 bytes and return...

Noxxik
1st March 2009, 23:02
thaks for your answer... I tried... but nothing changes :( programm crash in same time... I tried remove part with "magicnumber" but it crash too...
Could be mistake in another part of my code? Or I forget for something?