PDA

View Full Version : Object serialization with QDataStream



jahsiotr
20th January 2013, 11:52
Hello,

I am trying to overload operators << and >> for QDataStream and my class. I checked some tutorials and have been trying to use them for my issue and got it almost right. When I write to a file I believe it works properly as I checked a file with binary editor. It surely saves QStrings properly. I can't tell anything about QDate and QTime variables as they don't appear in understandable way in Hex Fiend. Also I am aware that constructor is not used properly as when it isn't commented out it makes my app crash. So any hints to make it proper would be greatly appreciated.


QDataStream &operator>>(QDataStream &in, Task &task) {

bool isDone;
QString name;
QString description;
QDate dayClicked;
QTime start;
QTime end;
bool neededReminder;
QDateTime reminderTime;
quint32 time;

in >> name >> description >> dayClicked >> start >> end >> reminderTime >> isDone >> neededReminder >> time;

qDebug() << name << description << dayClicked.toString() << start.toString() << end.toString()
<< reminderTime.toString() << isDone << neededReminder << time;

task = Task(isDone, name, description, dayClicked, start, end, (int)time, neededReminder);
return in;
}


What qDebug is returning: "" "" "" "00:00:00" "00:00:00" "" false false 0

ChrisW67
20th January 2013, 22:04
Also I am aware that constructor is not used properly as when it isn't commented out it makes my app crash. So any hints to make it proper would be greatly appreciated.
Constructor of what?


What qDebug is returning: "" "" "" "00:00:00" "00:00:00" "" false false 0
Is this incorrect? How are you creating the stream you are reading from? Are you reading the data from where you think you are reading from? Are the fields written to the stream in the same order you are reading them?