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 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 

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

    eekhoorn12 (13th June 2009)

  3. #2
    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 

  4. #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?

    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.

  5. #4
    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.

  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?

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

  7. #6
    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 

  8. #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?

    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 

  9. #8
    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 

  10. #9
    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?

  11. #10
    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?

    To test it i made the public.

  12. #11
    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?

    That error you're getting doesn't necessarily mean what you think it means, sometimes GCC errors are ambiguous.

    I copied all the code as-is into a project in my PC then added the two line I told you about (the declarations) and it turend out that you should un-hint the "#include <QDataStream>" and the error will go away...

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

    eekhoorn12 (14th June 2009)

  14. #12
    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 tried it and it works. Thanks everything works now.

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.