Serialize a class with a Qlist of custom classes as member (using QDataStream)

给你一囗甜甜゛ 提交于 2019-12-13 04:25:06

问题


I'm trying to serialize class Lesson(my custom class[I removed some setters]), which includes a QList(Question is also my custom class). During test serialization I get a SIGSEGV(segmentation fault) when deserializing any object of QList inside the class Lesson.

UPD1: I serialized QList* and now I changed it to QList, but SIGSEGV still appears.

class Lesson
{

public:
Lesson();
Lesson(QString, QString, QList<Question>);

QString getName() const;
QString getText() const;
QList<Question>* getTest() const;

friend QDataStream &operator<<(QDataStream &out, const Lesson &l){
    out << l.getName() << l.getText();
    out << l.getTest();
    return out;
}

friend QDataStream &operator>>(QDataStream &in, Lesson &l){
    QString name;
    QString text;
    QList<Question> t;
    in >> name >> text >> t;
    l = Lesson(name, text, t);
    return in;
}


friend QDataStream &operator<<(QDataStream &out, const Lesson *&l){
    out << l->getName() << l->getText();
    return out;
}

friend QDataStream &operator>>(QDataStream &in, Lesson *&l){
    QString name;
    QString text;
    QList<Question> test;
    in >> name >> text;// >> test;
    l = new Lesson(name, text, test);
    return in;
}


private:
QString text;
QList <Question>* test_;
QString name;



};

//class Question starts here

class Question
{

public:
explicit Question(QString, QList<QString>, int type, QList<int> ans);
Question();

friend QDataStream &operator <<(QDataStream& out, const Question &q){
    out << q.type << q.questText << q.variants << q.answer;
    return out;
}

friend QDataStream &operator >>(QDataStream& in, Question &q){
    int type;
    QString quesText;
    QList<QString> vars;
    QList<int>ans;
    in >> type >> q >> vars >> ans;
    q = Question(quesText, vars, type, ans);
    return in;
}

friend QDataStream &operator <<(QDataStream& out, const Question *&q){
    //out.setByteOrder(QSysInfo::ByteOrder);
    out << q->type << q->questText << q->variants << q->answer;
    return out;
}

friend QDataStream &operator >>(QDataStream& in, Question *&q){
    //in.setByteOrder(QSysInfo::ByteOrder);
    int type;
    QString quesText;
    QList<QString> vars;
    QList<int>ans;
    in >> type >> q >> vars >> ans;
    q = new Question(quesText, vars, type, ans);
    return in;
}

int gettype() const;
QString getquestText() const;
QList<QString> getvariants() const;
QList<int> getanswer() const;

signals:

public slots:

private:
int type; // see constants.h
QString questText;
QList<QString> variants;
QList<int> answer;


};

// main.cpp where I test serialization

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Core *c = new Core;
MainWindow w;
QObject::connect(&w, SIGNAL(sendCheckUserValidityToCore(QString,QString)), c,      SLOT(checkUserValidity(QString,QString)));
QObject::connect(&w, SIGNAL(sendLessonToCore(Lesson*)), c, SLOT(saveLesson(Lesson*)));
QObject::connect(&w, SIGNAL(requestLessonByIdfromCore(QString)), c, SLOT(getLessonById(QString)));
QObject::connect(c, SIGNAL(returnLessonById(Lesson*)), &w, SIGNAL(returnLessonByIdfromCore(Lesson*)));
QObject::connect(c, SIGNAL(entranceOff()), &w, SLOT(entranceOff()));

QFile tfile("lessons/tFile.stc");
tfile.open(QIODevice::ReadWrite);
QDataStream inout(&tfile);
QList<Question> qlistq;
Question q;
q.setQuestText("QWhy");
qlistq.insert(0, q);

Lesson lessont("hello", "welt", qlistq);
Lesson lessonout;
inout << lessont;
inout.device()->reset();
inout >> lessonout;
qDebug() << lessonout.getName();


return a.exec();
}

回答1:


You serialize a QList<Question>* and deserialize a QList<Question>. Change your operator<< to:

friend QDataStream &operator<<(QDataStream &out, const Lesson &l){
    out << name << text << *test_;
    return out;
}



回答2:


I found out what's wrong. I just didn't initialize all members of the class Question, that caused segfault



来源:https://stackoverflow.com/questions/23202667/serialize-a-class-with-a-qlist-of-custom-classes-as-member-using-qdatastream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!