本文介绍了使用自定义类的Qlist作为成员序列化一个类(使用QDataStream)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对课程Lesson(我的自定义类[我删除了一些设置器])进行序列化,其中包括一个QList(问题也是我的自定义类).在测试序列化过程中,在对课程Lesson中的QList的任何对象进行反序列化时,都会收到SIGSEGV(分段错误).

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:我序列化了QList *,现在将其更改为QList,但是仍然出现SIGSEGV.

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问题从这里开始

//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,我在其中测试序列化

// 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();
}

推荐答案

我发现了问题所在.我只是没有初始化Question类的所有成员,这导致了segfault

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

这篇关于使用自定义类的Qlist作为成员序列化一个类(使用QDataStream)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 21:51