I'am stuck.
I have a class with two QList
class Formulas
{
public:
Formulas();
~Formulas();
QList<QString> m_variables;
QList<QString> m_values;
};
QDataStream & operator<<
(QDataStream
& stream,
const Formulas
& formula
);
QDataStream & operator>>
(QDataStream
& stream, Formulas
& formula
);
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);
To copy to clipboard, switch view to plain text mode
QDataStream & operator<<
(QDataStream
& stream,
const Formulas
& formula
) {
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
) {
int size;
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
formulas_dlg
::formulas_dlg(QWidget *parent
) : m_ui(new Ui::formulas_dlg)
{
m_ui->setupUi(this);
.....
//QDataStream in(&file,&form_teste); // read the data serialized from the file
in<<form_teste;
file.close();
// form_teste is of type Formulas defined in formulas_dlg class
......
}
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);
......
}
To copy to clipboard, switch view to plain text mode
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
QDataStream out;
// we will serialize the data into the file out << form_teste;
file.close();
}
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();
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks