Results 1 to 4 of 4

Thread: QDataStream and saving and loading QList

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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.

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
  •  
Qt is a trademark of The Qt Company.