Results 1 to 20 of 24

Thread: How to write a QList into a binary file?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2008
    Location
    Qt Reference Documentation
    Posts
    62
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default How to write a QList into a binary file?

    How can I write a QList into a binary file since the QDataStream.operator<<() and QDataStream.operator>>() have no corresponding overload in which I can pass in a QList.

    In the application I am writing, there is a Patient class which I have a QList of and I want to read it from a binary file at startup and manipulate it throughout the execution, then at the end, write it back into the same file.

    Any advice?

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to write a QList into a binary file?

    it depends which type you use in template.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Dec 2008
    Location
    Qt Reference Documentation
    Posts
    62
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to write a QList into a binary file?

    Thanks for replying, the QList is of type Patient which is a class I defined, and I am getting an error when trying to write the list to a binary file, here is some code:
    Qt Code:
    1. class Patient
    2. {
    3. public:
    4. Patient();
    5. private:
    6. QString firstName;
    7. QString lastName;
    8. };
    9.  
    10. int main()
    11. {
    12. QList<Patient> patientList;
    13. QFile qFile("Patient_List.bin");
    14. if (qFile.open(QIODevice::ReadOnly))
    15. {
    16. QDataStream in(&qFile);
    17. in >> patientList; //error here
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to write a QList into a binary file?

    implement your own QDataStrem:: operator << & >> for this type and everything should work.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. The following user says thank you to spirit for this useful post:

    Lawand (31st March 2009)

  6. #5
    Join Date
    Dec 2008
    Location
    Qt Reference Documentation
    Posts
    62
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to write a QList into a binary file?

    You mean something like this:
    Qt Code:
    1. QDataStream& QDataStream::operator>>(Patient patient)
    2. {
    3. *this >> patient.firstName;
    4. *this >> patient.lastName;
    5.  
    6. return *this;
    7. }
    8.  
    9. QDataStream& QDataStream::operator<<(Patient patient)
    10. {
    11. *this << patient.firstName;
    12. *this << patient.lastName;
    13.  
    14. return *this;
    15. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to write a QList into a binary file?

    almost, I would use these signatures.
    Qt Code:
    1. QDataStream &operator<<(QDataStream &in, const Patient &patient)
    2. {
    3. ...
    4. }
    5.  
    6. QDataStream &operator>>(QDataStream &in, Patient &patient)
    7. {
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. The following user says thank you to spirit for this useful post:

    Carlton (30th July 2009)

  9. #7
    Join Date
    Dec 2008
    Location
    Qt Reference Documentation
    Posts
    62
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to write a QList into a binary file?

    Please bear with me ,
    I defined those in the patient.cpp file:
    Qt Code:
    1. QDataStream& QDataStream::operator<<(QDataStream &in, const Patient &patient)
    2. {
    3. in << patient.firstName;
    4. in << patient.lastName;
    5.  
    6. return in;
    7. }
    8.  
    9. QDataStream& QDataStream::operator>>(QDataStream &out, Patient &patient)
    10. {
    11. out >> patient.firstName;
    12. out >> patient.lastName;
    13.  
    14. return out;
    15. }
    To copy to clipboard, switch view to plain text mode 

    but I got these errors at the opening curly brace of operator<<:
    Qt Code:
    1. Multiple markers at this line
    2. - non-inline function 'QDataStream& QDataStream::operator<<(QDataStream&, const
    3. Patient&)' is defined after prior declaration as dllimport: attribute ignored
    4. - prototype for `QDataStream& QDataStream::operator<<(QDataStream&, const Patient&)'
    5. does not match any in class `QDataStream'
    6. - `QDataStream& QDataStream::operator<<(QDataStream&, const Patient&)' must take
    7. exactly one argument
    To copy to clipboard, switch view to plain text mode 

    and these errors at the opening curly brace of operator>>:
    Qt Code:
    1. Multiple markers at this line
    2. - `QDataStream& QDataStream::operator>>(QDataStream&, Patient&)' must take
    3. exactly one argument
    4. - prototype for `QDataStream& QDataStream::operator>>(QDataStream&, Patient&)'
    5. does not match any in class `QDataStream'
    To copy to clipboard, switch view to plain text mode 
    what's wrong?

  10. #8
    Join Date
    Feb 2009
    Location
    Venezuela
    Posts
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to write a QList into a binary file?

    Here:

    Qt Code:
    1. //spirit's code:
    2. QDataStream &operator<<(QDataStream &in, const Patient &patient)
    3.  
    4.  
    5. //Your code:
    6. QDataStream& QDataStream::operator<<(QDataStream &in, const Patient &patient)
    7. ^^^^^^^^^^^^^
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to JhonJames for this useful post:

    Lawand (31st March 2009)

  12. #9
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to write a QList into a binary file?

    try this example
    h - file
    Qt Code:
    1. class Foo
    2. {
    3. public:
    4. Foo(int number = 0, const QString &name = QString())
    5. : m_number(number), m_name(name)
    6. {}
    7.  
    8. void setNumber(int number) { m_number = number; }
    9. int number() const { return m_number; }
    10.  
    11. void setName(const QString &name) { m_name = name; }
    12. QString name() const { return m_name; }
    13.  
    14. private:
    15. int m_number;
    16. QString m_name;
    17. };
    18.  
    19. QDataStream &operator<<(QDataStream &out, const Foo &foo);
    20. QDataStream &operator>>(QDataStream &in, Foo &foo);
    To copy to clipboard, switch view to plain text mode 

    cpp-file
    Qt Code:
    1. QFile file("test.txt");
    2. if (!file.open(QIODevice::WriteOnly))
    3. return;
    4.  
    5. QDataStream out(&file);
    6. QList<Foo> res;
    7. for (int i = 0; i < 11; ++i) {
    8. Foo f(i, tr("name%1").arg(i));
    9. res << f;
    10. }
    11. out << res;
    12. file.close();
    13.  
    14. if (!file.open(QIODevice::ReadOnly))
    15. return;
    16.  
    17. QDataStream in(&file);
    18. res.clear();
    19. in >> res;
    20.  
    21. for (int i = 0; i < res.size(); ++i) {
    22. const Foo f = res.at(i);
    23. qDebug() << f.number() << " " << f.name();
    24. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  13. The following user says thank you to spirit for this useful post:

    Lawand (31st March 2009)

  14. #10
    Join Date
    Dec 2008
    Location
    Qt Reference Documentation
    Posts
    62
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to write a QList into a binary file?

    Thank you JhonJames and spirit, writing and reading the List are working!
    Last edited by Lawand; 31st March 2009 at 19:50.

  15. #11
    Join Date
    Jun 2007
    Location
    Netherlands
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to write a QList into a binary file?

    I have a similar kind of problem. I want to write a QList with custom classes to a binary file.

    I want to write a
    Qt Code:
    1. static QList<Server> serversList;
    To copy to clipboard, switch view to plain text mode 
    to a binary file

    I my server.cpp i have the following lines of code:
    Qt Code:
    1. QDataStream& operator<<(QDataStream& out, const Server& server)
    2. {
    3. out << server.serverAddres;
    4. ...
    5. out << server.serverUseSsl;
    6. return out;
    7. }
    8.  
    9. QDataStream& operator>>(QDataStream& in,Server& server)
    10. {
    11. in >> server.serverAddres;
    12. ...
    13. in >> server.serverUseSsl;
    14. return in;
    15. }
    To copy to clipboard, switch view to plain text mode 

    In the file serverlist.cpp i do the operation:
    Qt Code:
    1. if(!serverDataFile.open(QIODevice::ReadWrite))
    2. {
    3. return;
    4. }
    5.  
    6. outputStream = new QDataStream(&serverDataFile);
    7. outputStream << listofServers;
    To copy to clipboard, switch view to plain text mode 

    but when i compile i get the following error;
    Qt Code:
    1. O:/projects/Post_Program/serverlist.cpp:44: error: no match for 'operator<<' in '((serverList*)this)->serverList::outputStream << serverList::listofServers'
    To copy to clipboard, switch view to plain text mode 

  16. #12
    Join Date
    Dec 2008
    Location
    Qt Reference Documentation
    Posts
    62
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to write a QList into a binary file?

    I think outputStream should be a QDataStream instead of QDataStream* for your code to work...

    Or (if it has to be a QDataStream*) you gotta find another to call operator<<() such as:
    Qt Code:
    1. (*outputStream) << listofServers
    To copy to clipboard, switch view to plain text mode 

  17. The following user says thank you to Lawand for this useful post:

    eekhoorn12 (13th June 2009)

Similar Threads

  1. cannot execute binary file
    By mgturner in forum Installation and Deployment
    Replies: 1
    Last Post: 16th March 2009, 17:04
  2. Read binary file and convert to QString
    By jaca in forum Qt Programming
    Replies: 12
    Last Post: 13th June 2008, 23:05
  3. About File write operation
    By raghvendramisra in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2008, 11:41
  4. FSWriteFork in MAC OS to write data to a file.
    By vishal.chauhan in forum General Programming
    Replies: 5
    Last Post: 2nd July 2007, 06:48
  5. Sending Binary File with QFTP
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 7th March 2007, 18:10

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.