PDA

View Full Version : QDataStream-serialization not writting to file



john_god
30th July 2009, 19:23
I'am stuck.
I have a class with two QList


class Formulas
{
public:
Formulas();
~Formulas();

QString m_formula;
QList<QString> m_variables;
QList<QString> m_values;
};

QDataStream & operator<< (QDataStream& stream, const Formulas& formula);
QDataStream & operator>> (QDataStream& stream, Formulas& formula);



QDataStream & operator<< (QDataStream& stream, const Formulas& formula)
{
QString str;
stream<<formula.m_formula;
stream<<str.setNum(formula.m_variables.size());

for(int i=0;i<formula.m_variables.size();i++)
{
stream<<formula.m_values.at(i);
stream<<formula.m_variables.at(i);
}

return stream;
}

//QList<Formulas> lista_formulas;

QDataStream & operator>> (QDataStream& stream, Formulas& formula)
{
QString str;
int size;
QString val;
QString var;
stream>>formula.m_formula;
stream>>str;
size=str.toInt();

for(int i=0;i<size;i++)
{
stream>>val;
formula.m_values.append(val);
stream>>var;
formula.m_variables.append(var);
}

return stream;
}




formulas_dlg::formulas_dlg(QWidget *parent) :
QDialog(parent),
m_ui(new Ui::formulas_dlg)
{
m_ui->setupUi(this);
.....
QFile file("file.dat");
file.open(QIODevice::ReadOnly);
//QDataStream in(&file,&form_teste); // read the data serialized from the file
QDataStream in;
in<<form_teste;
file.close();
// form_teste is of type Formulas defined in formulas_dlg class
QMessageBox::about(this,"",form_teste.m_formula);
......
}



void formulas_dlg::on_buttonBox_accepted()
{
QMessageBox::about(this,"","ok");// check if this function is called

form_teste.m_formula="xpto";//check if this data is written to file, and it's not file stays empty

QFile file("file.dat");
file.open(QIODevice::WriteOnly);
QDataStream out; // we will serialize the data into the file
out << form_teste;
file.close();

}

it compiles without erros, but the file is still empty after on_buttonBox_accepted is called.
Any ideas to solve this????????????????

Bta, this is the easy part. I really dont want to serialize the type "Formulas form_teste"
but the type "QList<Formulas> form_teste";
I am just doing it iin steps and trying to solve this error first.

Thank you

nish
31st July 2009, 02:01
you have over complicated things..

1. Use QStringList instead of QList<QString>,
2. You do not have to insert the count of stringlist (even if you are using QList) ..
just do stream<<stringlist ;
3. The Datastream works on a file...
QFile file(...);
file.open(...);
QDataStream st(&file);//now st will work on file.

rest will do.

john_god
31st July 2009, 18:30
Hi MDeath.

It works fine now.
But now Im stuck with the QList<Formula> serialization. My code:



QDataStream & operator<< (QDataStream& stream, const Formulas& formula);
QDataStream & operator>> (QDataStream& stream, Formulas& formula);

QDataStream & operator<< (QDataStream& stream, const QList<Formulas>& list_formula);
QDataStream & operator>> (QDataStream& stream, const QList<Formulas>& list_formula);



QDataStream & operator<< (QDataStream& stream, const Formulas& formula)
{
stream<<formula.m_formula;
stream<<formula.m_values;
stream<<formula.m_variables;
return stream;
}

QDataStream & operator>> (QDataStream& stream, Formulas& formula)
{
stream>>formula.m_formula;
stream>>formula.m_values;
stream>>formula.m_variables;
return stream;
}

QDataStream & operator<< (QDataStream& stream, QList<Formulas>& list_formula)
{
stream<<list_formula;
return stream;
}

QDataStream & operator>> (QDataStream& stream, QList<Formulas>& list_formula)
{
stream>>list_formula;
return stream;
}


reading the file


QFile file("file.dat");
file.open(QIODevice::ReadOnly);
//QDataStream in(&file,&form_teste); // read the data serialized from the file
QDataStream in(&file);
//in>>form_teste;
in>>lista_formulas;
file.close();


writting the file



QFile file("file.dat");
file.open(QIODevice::WriteOnly);
QDataStream out(&file); // we will serialize the data into the file
//out << form_teste;
out<<lista_formulas;
file.close();


the error:


D:/qt4examples/Matematica/./debug/formulas_dlg.o:-1: error: In function `ZN12formulas_dlg21on_buttonBox_acceptedEv':
D:/qt4examples/Matematica/formulas_dlg.cpp:58: undefined reference to `operator<<(QDataStream&, QList<Formulas> const&)'

if I put a comment in the lines


in>>lista_formulas;
out<<lista_formulas;
it compiles and run fine

nish
1st August 2009, 01:16
you have declared the QList as "const" in the prototype of function, but in the definition you forgot to put the "const"

john_god
1st August 2009, 12:27
Thanks MrDeath, I fix the const, but doenst work. Compiles fine and runs, but crashes when when reading and writting. The file his created but is always empty.
In a act of dispear:crying: I try again storing the size of QList<Formulas>, then a for() and it works:o. Any ideas ???