Results 1 to 3 of 3

Thread: Qlist Serialization by qdatastream error

  1. #1
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Qlist Serialization by qdatastream error

    Hi,
    I try to serialize my own class, which inherits from QGraphicsItem and QObject, but i have a problem when i want to read it from file:

    Qt Code:
    1. #include "zarzadcaodczytu.h"
    2.  
    3. #include <QMetaProperty>
    4. #include <QFile>
    5. #include <QMetaType>
    6. #include <qdebug.h>
    7.  
    8. #include "../editorwindow.h"
    9. #include "ui_editorwindow.h"
    10.  
    11.  
    12.  
    13. ZarzadcaOdczytu::ZarzadcaOdczytu()
    14. {
    15. }
    16.  
    17. bool ZarzadcaOdczytu::wczytaj(QString nazwaPliku, EditorWindow *edytor)
    18. {
    19. //HBMalyMis and HBDuzyMis inherits from HoneyBearsMapItem
    20. qRegisterMetaType<HBMalyMis>("HBMalyMis");
    21. qRegisterMetaType<HBDuzyMis>("HBDuzyMis");
    22.  
    23. QFile file(nazwaPliku);
    24. file.open(QIODevice::ReadOnly);
    25. QDataStream in(&file);
    26. QList< HoneyBearsMapItem * > items;
    27.  
    28. //in debugger i can see that every objects exists and values are set properly
    29. in >> items;
    30. file.close();
    31.  
    32. delete edytor->ui->graphicsView->scene();
    33.  
    34. //PlanszaScene inherits from QGraphicsScene
    35. PlanszaScene * nowa = new PlanszaScene(1336, 640, edytor);
    36. edytor->ui->graphicsView->setScene(nowa);
    37.  
    38.  
    39. qDebug() << items.count();
    40.  
    41. for(HoneyBearsMapItem *item : items){
    42. nowa->addItem(item); // <- in this line app crash, when I try to call metaObject() app crash too
    43. }
    44.  
    45.  
    46. return true;
    47. }
    48.  
    49. HoneyBearsMapItem *ZarzadcaOdczytu::create(const char *classname)
    50. {
    51. QString name(classname);
    52.  
    53. if(name == "HBDuzyMis")
    54. return new HBDuzyMis();
    55. if(name == "HBMalyMis")
    56. return new HBMalyMis();
    57.  
    58. return NULL;
    59. }
    60.  
    61. QDataStream &operator>>(QDataStream &in, HoneyBearsMapItem *item)
    62. {
    63. char * className;
    64. in >> className;
    65.  
    66. item = ZarzadcaOdczytu().create(className);
    67.  
    68. const QMetaObject *meta = item->metaObject();
    69. int count = meta->propertyCount();
    70. for (int i=0; i<count; ++i) {
    71. QMetaProperty metaproperty = meta->property(i);
    72. if (metaproperty.isWritable()){
    73. const char *name = metaproperty.name();
    74. QVariant value;
    75. in >> value;
    76. item->setProperty(name, value);
    77.  
    78. //every values are read properly
    79. qDebug() << "Property " << name << " " << value;
    80.  
    81. }
    82.  
    83. }
    84.  
    85.  
    86. return in;
    87.  
    88. }
    To copy to clipboard, switch view to plain text mode 

    What I'm doing wrong :/ ? In the operator >> function I can call metaObject(), but when I try do this outside this function after it, the application crash. Other objects methods works fine. The main problem is when I try add object to scene :/

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qlist Serialization by qdatastream error

    There are all kinds of fishy things. For example who frees className or why does the operator ignore the passed in item?

    My recommendation would be to separate serialization from the item.

    For example like this

    Qt Code:
    1. class MyItem : public QGraphicsItem
    2. {
    3. public:
    4. QVariantMap serialize() const;
    5.  
    6. static MyItem* deserizalize(const QVariantMap &data);
    7. }
    To copy to clipboard, switch view to plain text mode 

    Used like this
    Qt Code:
    1. QDataStream stream(...);
    2. foreach (MyItem* item, items) {
    3. stream << item->serialize();
    4. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QDataStream stream(...);
    2. while (!stream.atEnd()) {
    3. QVariantMap data;
    4. stream >> data;
    5.  
    6. items << MyItem::deserialize(data);
    7. }
    To copy to clipboard, switch view to plain text mode 

    For the classname I would suggest to either use QByteArray or to have an entry for that in the QVariantMap (but also a QByteArray to avoid manual memory management)

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    WezSieTato (4th January 2014)

  4. #3
    Join Date
    Aug 2014
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qlist Serialization by qdatastream error

    I found a way to handle this. This may be non standard and need template specialization.
    I created a separate header file with the template and included it wherever required. Here it is. In case you need other data structures to be serialized, you may follow the similar pattern.

    Cheers !

    #ifndef MODSER_H
    #define MODSER_H
    #include <QDataStream>

    template <typename T>
    QDataStream& operator>>(QDataStream& s, QList<T*>& l)
    {
    l.clear();
    quint32 c;
    s >> c;
    l.reserve(c);
    for(quint32 i = 0; i < c; ++i)
    {
    T * t = new T;
    s >> t;
    l.append(t);
    if (s.atEnd())
    break;
    }
    return s;
    }

    #endif // MODSER_H

Similar Threads

  1. Object serialization with QDataStream
    By jahsiotr in forum Newbie
    Replies: 1
    Last Post: 20th January 2013, 23:04
  2. QWebKit, QDataStream, QList
    By Viper666 in forum Qt Programming
    Replies: 6
    Last Post: 21st September 2012, 14:49
  3. QDataStream and serialization
    By pdoria in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2009, 10:42
  4. QDataStream-serialization not writting to file
    By john_god in forum Qt Programming
    Replies: 4
    Last Post: 1st August 2009, 13:27
  5. [Qt4] qdatastream/qstring serialization
    By fabo in forum Qt Programming
    Replies: 4
    Last Post: 19th April 2006, 19:31

Tags for this Thread

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.