Page 1 of 2 12 LastLast
Results 1 to 20 of 24

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

  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 20: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)

  18. #13
    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?

    Thanks that solved the problem but created a new one.
    I now get the following error:
    Qt Code:
    1. f:/Qt/2009.02/qt/include/QtCore/../../src/corelib/io/qdatastream.h: In function 'QDataStream& operator<<(QDataStream&, const QList<T>&) [with T = Server]':
    2. serverlist.cpp:45: instantiated from here
    3. f:/Qt/2009.02/qt/include/QtCore/../../src/corelib/io/qdatastream.h:252: error: no match for 'operator<<' in 's << (+l)->QList<T>::at [with T = Server](i)'
    To copy to clipboard, switch view to plain text mode 

  19. #14
    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?

    What are the types of server.serverAddres and server.serverUseSsl? because you might have to overload operator<<() and operator>>() on them...
    As the Assistant puts it:
    The QDataStream class implements the serialization of C++'s basic data types, like char, short, int, char *, etc. Serialization of more complex data is accomplished by breaking up the data into primitive units.

  20. #15
    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?

    Server consits of a few QString's some bool's and qint32.

  21. #16
    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?

    Would you post the full code of the serverlist.cpp file

  22. #17
    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?

    This is the serverlist.cpp:
    Qt Code:
    1. #include <QDir>
    2. #include <QMessageBox>
    3.  
    4. #include "serverlist.h"
    5.  
    6. QList<Server> listofServers;
    7.  
    8.  
    9. serverList::serverList()
    10. {
    11. settings = new Settings();
    12. QFile serverDataFile(settings->getappDataDir() + QDir::separator() + "servers" + QDir::separator() + "servers.dat");
    13. serverDataFileInfo = new QFileInfo(serverDataFile);
    14. QDataStream outputStream(&serverDataFile);
    15. QDataStream inputStream(&serverDataFile);
    16. }
    17.  
    18. void serverList::saveServers()
    19. {
    20. if(serverDataFileInfo->exists())
    21. {
    22. QDir newFile(serverDataFileInfo->absoluteFilePath());
    23. newFile.mkpath(serverDataFileInfo->absolutePath());
    24. }
    25. if(!serverDataFile.open(QIODevice::ReadWrite))
    26. {
    27. QMessageBox::critical( 0, "Post program","There was a problem reading the server settings file" );
    28. }
    29. outputStream << listofServers;
    30. }
    31.  
    32. void serverList::addServer(Server newServer)
    33. {
    34. }
    To copy to clipboard, switch view to plain text mode 

    This is the serverlist.h:
    Qt Code:
    1. #ifndef SERVERLIST_H
    2. #define SERVERLIST_H
    3.  
    4. #include <QList>
    5. #include <QFile>
    6. #include <QFileInfo>
    7. #include <QDataStream>
    8.  
    9. #include <server.h>
    10. #include <settings.h>
    11.  
    12. class serverList
    13. {
    14. public:
    15. serverList();
    16. void saveServers();
    17. void addServer(Server newServer);
    18. void loadServers();
    19.  
    20. private:
    21. static QList<Server> listofServers;
    22. Settings *settings;
    23. QFileInfo *serverDataFileInfo;
    24. QFile serverDataFile;
    25. QDataStream outputStream;
    26. QDataStream inputStream;
    27. };
    28.  
    29. #endif // SERVERLIST_H
    To copy to clipboard, switch view to plain text mode 

    I will post the Server class also to get the complete picture
    server.cpp:
    Qt Code:
    1. #include "server.h"
    2. //#include <QDataStream>
    3.  
    4. Server::Server()
    5. {
    6. }
    7.  
    8. void Server::setAddres(QString newAddres)
    9. {
    10. serverAddres = newAddres;
    11. }
    12.  
    13. void Server::setNickname(QString newNickname)
    14. {
    15. serverNickname = newNickname;
    16. }
    17.  
    18. void Server::setPort(qint32 newPort)
    19. {
    20. serverPort = newPort;
    21. }
    22.  
    23. void Server::setConnections(qint32 newConnections)
    24. {
    25. serverConnections = newConnections;
    26. }
    27.  
    28. void Server::setUsername(QString newUsername)
    29. {
    30. serverUsername = newUsername;
    31. }
    32.  
    33. void Server::setPassword(QString newPassword)
    34. {
    35. serverPassword = newPassword;
    36. }
    37.  
    38. void Server::setRequireLogin(bool newRequireLogin)
    39. {
    40. serverRequireLogin = newRequireLogin;
    41. }
    42.  
    43. void Server::setUseSsl(bool newUseSsl)
    44. {
    45. serverUseSsl = newUseSsl;
    46. }
    47.  
    48. QString Server::getAddres()
    49. {
    50. return serverAddres;
    51. }
    52.  
    53. QString Server::getNickname()
    54. {
    55. return serverNickname;
    56. }
    57.  
    58. qint32 Server::getPort()
    59. {
    60. return serverPort;
    61. }
    62.  
    63. qint32 Server::getConnections()
    64. {
    65. return serverConnections;
    66. }
    67.  
    68. QString Server::getUsername()
    69. {
    70. return serverUsername;
    71. }
    72.  
    73. QString Server::getPassword()
    74. {
    75. return serverPassword;
    76. }
    77.  
    78. bool Server::getRequireLogin()
    79. {
    80. return serverRequireLogin;
    81. }
    82.  
    83. bool Server::getUseSsl()
    84. {
    85. return serverUseSsl;
    86. }
    87.  
    88.  
    89.  
    90. QDataStream& operator<<(QDataStream& out, const Server& server)
    91. {
    92. out << server.serverAddres;
    93. out << server.serverNickname;
    94. out << server.serverPort;
    95. out << server.serverConnections;
    96. out << server.serverUsername;
    97. out << server.serverPassword;
    98. out << server.serverRequireLogin;
    99. out << server.serverUseSsl;
    100. return out;
    101. }
    102. QDataStream& operator>>(QDataStream& in,Server& server)
    103. {
    104. in >> server.serverAddres;
    105. in >> server.serverNickname;
    106. in >> server.serverPort;
    107. in >> server.serverConnections;
    108. in >> server.serverUsername;
    109. in >> server.serverPassword;
    110. in >> server.serverRequireLogin;
    111. in >> server.serverUseSsl;
    112. return in;
    113. }
    To copy to clipboard, switch view to plain text mode 

    server.h
    Qt Code:
    1. #ifndef SERVER_H
    2. #define SERVER_H
    3.  
    4. #include <QString>
    5. #include <QtGlobal>
    6.  
    7.  
    8. class Server
    9. {
    10. public:
    11. Server();
    12.  
    13. //Set functions
    14. void setAddres(QString newAddres);
    15. void setNickname(QString newNickname);
    16. void setPort(qint32 newPort);
    17. void setConnections(qint32 newConnections);
    18. void setUsername(QString newUsername);
    19. void setPassword(QString newPassword);
    20. void setRequireLogin(bool newRequireLogin);
    21. void setUseSsl(bool newUseSsl);
    22.  
    23. //Get functions
    24. QString getAddres();
    25. QString getNickname();
    26. qint32 getPort();
    27. qint32 getConnections();
    28. QString getUsername();
    29. QString getPassword();
    30. bool getRequireLogin();
    31. bool getUseSsl();
    32.  
    33. private:
    34. QString serverAddres;
    35. QString serverNickname;
    36. qint32 serverPort;
    37. qint32 serverConnections;
    38. QString serverUsername;
    39. QString serverPassword;
    40. bool serverRequireLogin;
    41. bool serverUseSsl;
    42. };
    43. #endif // SERVER_H
    To copy to clipboard, switch view to plain text mode 

  23. #18
    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?

    In my case, I declared the functions operator<<() and operator>>() in the .h file and implemented them in the .cpp file, and when I tried removing the declarations from the .h file I got the very same error you got.

    So, put those 2 lines in the server.h file:
    Qt Code:
    1. QDataStream& operator<<(QDataStream& out, const Server& server);
    2. QDataStream& operator>>(QDataStream& in,Server& server);
    To copy to clipboard, switch view to plain text mode 

  24. #19
    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?

    Thanks that worked. Now i have to look at my implementation of the reading of a file because it is complaining about a missing operator at the bool's and qint32's but they should be available according to the documentation.

    Qt Code:
    1. O:/projects/Post_Program/server.cpp:107: error: no match for 'operator>>' in 'in >> server->Server::serverPort'
    To copy to clipboard, switch view to plain text mode 

  25. #20
    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?

    Wait a minute, aren't those members private? how are you accessing them directly?

Similar Threads

  1. cannot execute binary file
    By mgturner in forum Installation and Deployment
    Replies: 1
    Last Post: 16th March 2009, 18:04
  2. Read binary file and convert to QString
    By jaca in forum Qt Programming
    Replies: 12
    Last Post: 14th June 2008, 00:05
  3. About File write operation
    By raghvendramisra in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2008, 12: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, 07:48
  5. Sending Binary File with QFTP
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 7th March 2007, 19: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.