Results 1 to 4 of 4

Thread: QDataStream and saving and loading QList

  1. #1
    Join Date
    Feb 2009
    Posts
    53
    Thanks
    5

    Default QDataStream and saving and loading QList

    Hi I am trying to save QList into QDataStream and reading from QDataStream.
    I have this code.
    writing:
    Qt Code:
    1. QFile file(fileName);
    2. if (!file.open(QIODevice::WriteOnly))
    3. return false;
    4.  
    5. QDataStream outStream(&file);
    6. outStream.setVersion(QDataStream::Qt_4_4);
    7.  
    8. outStream << (quint32)0xA0B0C0D0;
    9. outStream << (qint32)123;
    10.  
    11. outStream << list;
    To copy to clipboard, switch view to plain text mode 
    It seems good, but when I try to read file like this.
    Qt Code:
    1. QFile file(fileName);
    2. if(!file.open(QIODevice::ReadOnly))
    3. return false;
    4.  
    5. QDataStream inStream(&file);
    6. inStream.setVersion(QDataStream::Qt_4_4);
    7.  
    8. quint32 magicWord;
    9. inStream >> magicWord;
    10. if (magicWord != 0xA0B0C0D0)
    11. return false;
    12.  
    13. while(!file.atEnd())
    14. {
    15. inStream >> list;
    16. }
    To copy to clipboard, switch view to plain text mode 
    list is private QList of my items. I thought that it will be OK according THIS, but it isnot Could someone help me please, how can I solve this?

  2. #2
    Join Date
    Feb 2009
    Posts
    53
    Thanks
    5

    Default Re: QDataStream and saving and loading QList

    I found in google that this is not good.
    So I try this

    Qt Code:
    1. bool MainScene::loadFile(const QString &fileName)
    2. {
    3. QFile file(fileName);
    4. if(!file.open(QIODevice::ReadOnly))
    5. return false;
    6.  
    7. QDataStream inStream(&file);
    8. inStream.setVersion(QDataStream::Qt_4_4);
    9.  
    10. quint32 magicWord, second;
    11. inStream >> magicWord >> second;
    12. if (magicWord != 0xA0B0C0D0) return false;
    13.  
    14. QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); // set wait cursor during loading
    15.  
    16. inStream >> list;
    17. QApplication::restoreOverrideCursor();
    18. file.close();
    19. return true;
    20. }
    21.  
    22. bool MainScene::saveFile(const QString &fileName)
    23. {
    24. QFile file(fileName);
    25. if (!file.open(QIODevice::WriteOnly))
    26. return false;
    27.  
    28. QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); // set wait cursor during saving
    29.  
    30. QDataStream outStream(&file);
    31. outStream.setVersion(QDataStream::Qt_4_4);
    32.  
    33. outStream << (quint32)0xA0B0C0D0;
    34. outStream << (qint32)123;
    35.  
    36. outStream << list;
    37. QApplication::restoreOverrideCursor();
    38. file.close();
    39. return true;
    40. }
    41.  
    42.  
    43. QDataStream &operator<<(QDataStream &out, const Item *item)
    44. {
    45. out << qint16(item->itemType()) << item->sceneBoundingRect() << item->pen() << item->brush() << item->pos() << item->zValue();
    46. return out;
    47. }
    48.  
    49. QDataStream &operator>>(QDataStream &in, Item *item)
    50. {
    51. qint16 type;
    52. QRectF rect;
    53. QPen pen;
    54. QBrush brush;
    55. QPointF point;
    56. int zValue;
    57.  
    58. in >> type >> rect >> pen >> brush >> point >> zValue;
    59. item = new Item((Item::ItemType)type, rect);
    60. item->setPen(pen);
    61. item->setBrush(brush);
    62. item->setPos(point);
    63. item->setZValue(zValue);
    64. return in;
    65. }
    To copy to clipboard, switch view to plain text mode 

    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? It could be hard, isnt it?
    Last edited by Noxxik; 1st March 2009 at 20:55.

  3. #3
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream and saving and loading QList

    Your reading doesn't match the writing, change it to:
    Qt Code:
    1. QFile file(fileName);
    2. if(!file.open(QIODevice::ReadOnly))
    3. return false;
    4.  
    5. QDataStream inStream(&file);
    6. inStream.setVersion(QDataStream::Qt_4_4);
    7.  
    8. quint32 magicWord;
    9. inStream >> magicWord;
    10. if (magicWord != 0xA0B0C0D0)
    11. return false;
    12.  
    13. qint32 id;
    14. inStream >> id; // should be 123
    15.  
    16. inStream >> list; // read the list saved by outStream >> list;
    To copy to clipboard, switch view to plain text mode 

    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...

  4. #4
    Join Date
    Feb 2009
    Posts
    53
    Thanks
    5

    Default Re: QDataStream and saving and loading QList

    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?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.