Results 1 to 5 of 5

Thread: QDataStream-serialization not writting to file

  1. #1
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Question QDataStream-serialization not writting to file

    I'am stuck.
    I have a class with two QList

    Qt Code:
    1. class Formulas
    2. {
    3. public:
    4. Formulas();
    5. ~Formulas();
    6.  
    7. QString m_formula;
    8. QList<QString> m_variables;
    9. QList<QString> m_values;
    10. };
    11.  
    12. QDataStream & operator<< (QDataStream& stream, const Formulas& formula);
    13. QDataStream & operator>> (QDataStream& stream, Formulas& formula);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QDataStream & operator<< (QDataStream& stream, const Formulas& formula)
    2. {
    3. QString str;
    4. stream<<formula.m_formula;
    5. stream<<str.setNum(formula.m_variables.size());
    6.  
    7. for(int i=0;i<formula.m_variables.size();i++)
    8. {
    9. stream<<formula.m_values.at(i);
    10. stream<<formula.m_variables.at(i);
    11. }
    12.  
    13. return stream;
    14. }
    15.  
    16. //QList<Formulas> lista_formulas;
    17.  
    18. QDataStream & operator>> (QDataStream& stream, Formulas& formula)
    19. {
    20. QString str;
    21. int size;
    22. QString val;
    23. QString var;
    24. stream>>formula.m_formula;
    25. stream>>str;
    26. size=str.toInt();
    27.  
    28. for(int i=0;i<size;i++)
    29. {
    30. stream>>val;
    31. formula.m_values.append(val);
    32. stream>>var;
    33. formula.m_variables.append(var);
    34. }
    35.  
    36. return stream;
    37. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. formulas_dlg::formulas_dlg(QWidget *parent) :
    2. QDialog(parent),
    3. m_ui(new Ui::formulas_dlg)
    4. {
    5. m_ui->setupUi(this);
    6. .....
    7. QFile file("file.dat");
    8. file.open(QIODevice::ReadOnly);
    9. //QDataStream in(&file,&form_teste); // read the data serialized from the file
    10. in<<form_teste;
    11. file.close();
    12. // form_teste is of type Formulas defined in formulas_dlg class
    13. QMessageBox::about(this,"",form_teste.m_formula);
    14. ......
    15. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void formulas_dlg::on_buttonBox_accepted()
    2. {
    3. QMessageBox::about(this,"","ok");// check if this function is called
    4.  
    5. form_teste.m_formula="xpto";//check if this data is written to file, and it's not file stays empty
    6.  
    7. QFile file("file.dat");
    8. file.open(QIODevice::WriteOnly);
    9. QDataStream out; // we will serialize the data into the file
    10. out << form_teste;
    11. file.close();
    12.  
    13. }
    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

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDataStream-serialization not writting to file

    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.

  3. The following user says thank you to nish for this useful post:

    john_god (31st July 2009)

  4. #3
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: QDataStream-serialization not writting to file

    Hi MDeath.

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


    Qt Code:
    1. QDataStream & operator<< (QDataStream& stream, const Formulas& formula);
    2. QDataStream & operator>> (QDataStream& stream, Formulas& formula);
    3.  
    4. QDataStream & operator<< (QDataStream& stream, const QList<Formulas>& list_formula);
    5. QDataStream & operator>> (QDataStream& stream, const QList<Formulas>& list_formula);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QDataStream & operator<< (QDataStream& stream, const Formulas& formula)
    2. {
    3. stream<<formula.m_formula;
    4. stream<<formula.m_values;
    5. stream<<formula.m_variables;
    6. return stream;
    7. }
    8.  
    9. QDataStream & operator>> (QDataStream& stream, Formulas& formula)
    10. {
    11. stream>>formula.m_formula;
    12. stream>>formula.m_values;
    13. stream>>formula.m_variables;
    14. return stream;
    15. }
    16.  
    17. QDataStream & operator<< (QDataStream& stream, QList<Formulas>& list_formula)
    18. {
    19. stream<<list_formula;
    20. return stream;
    21. }
    22.  
    23. QDataStream & operator>> (QDataStream& stream, QList<Formulas>& list_formula)
    24. {
    25. stream>>list_formula;
    26. return stream;
    27. }
    To copy to clipboard, switch view to plain text mode 

    reading the file
    Qt Code:
    1. QFile file("file.dat");
    2. file.open(QIODevice::ReadOnly);
    3. //QDataStream in(&file,&form_teste); // read the data serialized from the file
    4. QDataStream in(&file);
    5. //in>>form_teste;
    6. in>>lista_formulas;
    7. file.close();
    To copy to clipboard, switch view to plain text mode 

    writting the file

    Qt Code:
    1. QFile file("file.dat");
    2. file.open(QIODevice::WriteOnly);
    3. QDataStream out(&file); // we will serialize the data into the file
    4. //out << form_teste;
    5. out<<lista_formulas;
    6. file.close();
    To copy to clipboard, switch view to plain text mode 

    the error:

    Qt Code:
    1. D:/qt4examples/Matematica/./debug/formulas_dlg.o:-1: error: In function `ZN12formulas_dlg21on_buttonBox_acceptedEv':
    2. D:/qt4examples/Matematica/formulas_dlg.cpp:58: undefined reference to `operator<<(QDataStream&, QList<Formulas> const&)'
    To copy to clipboard, switch view to plain text mode 

    if I put a comment in the lines
    Qt Code:
    1. in>>lista_formulas;
    2. out<<lista_formulas;
    To copy to clipboard, switch view to plain text mode 
    it compiles and run fine

  5. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDataStream-serialization not writting to file

    you have declared the QList as "const" in the prototype of function, but in the definition you forgot to put the "const"

  6. The following user says thank you to nish for this useful post:

    john_god (1st August 2009)

  7. #5
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: QDataStream-serialization not writting to file

    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 I try again storing the size of QList<Formulas>, then a for() and it works. Any ideas ???

Similar Threads

  1. Read binary from file
    By weldpua2008 in forum Newbie
    Replies: 2
    Last Post: 4th April 2009, 00:50
  2. Can you specify a file engine?
    By skimber in forum Qt Programming
    Replies: 2
    Last Post: 18th September 2008, 16:54
  3. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 07:51
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 16:21
  5. QDataStream
    By frankoz in forum Qt Programming
    Replies: 1
    Last Post: 1st March 2006, 08:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.