使用自定义类的 Qlist 作为成员序列化一个类(使用 QDataStream)
Posted
技术标签:
【中文标题】使用自定义类的 Qlist 作为成员序列化一个类(使用 QDataStream)【英文标题】:Serialize a class with a Qlist of custom classes as member (using QDataStream) 【发布时间】:2014-04-21 17:45:19 【问题描述】:我正在尝试序列化课程课程(我的自定义课程[我删除了一些设置器]),其中包括一个 QList(问题也是我的自定义课程)。在测试序列化期间,当反序列化课程中的任何 QList 对象时,我得到一个 SIGSEGV(分段错误)。
UPD1:我序列化了 QList*,现在我将其更改为 QList,但仍然出现 SIGSEGV。
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
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
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】:您序列化 QList<Question>*
并反序列化 QList<Question>
。将您的 operator<<
更改为:
friend QDataStream &operator<<(QDataStream &out, const Lesson &l)
out << name << text << *test_;
return out;
【讨论】:
我将成员更改为 QList我发现了问题所在。我只是没有初始化Question类的所有成员,这导致了segfault
【讨论】:
以上是关于使用自定义类的 Qlist 作为成员序列化一个类(使用 QDataStream)的主要内容,如果未能解决你的问题,请参考以下文章