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?
Re: How to write a QList into a binary file?
it depends which type you use in template.
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:
Code:
class Patient
{
public:
Patient();
private:
};
int main()
{
QList<Patient> patientList;
QFile qFile
("Patient_List.bin");
{
in >> patientList; //error here
}
}
Re: How to write a QList into a binary file?
implement your own QDataStrem:: operator << & >> for this type and everything should work.
Re: How to write a QList into a binary file?
You mean something like this:
Code:
{
*this >> patient.firstName;
*this >> patient.lastName;
return *this;
}
{
*this << patient.firstName;
*this << patient.lastName;
return *this;
}
Re: How to write a QList into a binary file?
almost, I would use these signatures. :)
Re: How to write a QList into a binary file?
Please bear with me :),
I defined those in the patient.cpp file:
Code:
{
in << patient.firstName;
in << patient.lastName;
return in;
}
{
out >> patient.firstName;
out >> patient.lastName;
return out;
}
but I got these errors at the opening curly brace of operator<<:
Code:
Multiple markers at this line
- non-inline function 'QDataStream& QDataStream::operator<<(QDataStream&, const
Patient&)' is defined after prior declaration as dllimport: attribute ignored
- prototype
for `QDataStream
& QDataStream::operator<<
(QDataStream
&,
const Patient
&)' does not match any in class `QDataStream'
- `QDataStream
& QDataStream::operator<<
(QDataStream
&,
const Patient
&)' must take exactly one argument
and these errors at the opening curly brace of operator>>:
Code:
Multiple markers at this line
- `QDataStream
& QDataStream::operator>>
(QDataStream
&, Patient
&)' must take exactly one argument
- prototype for `QDataStream& QDataStream::operator>>(QDataStream&, Patient&)'
what's wrong? :(
Re: How to write a QList into a binary file?
Here:
Code:
//spirit's code:
//Your code:
^^^^^^^^^^^^^
Re: How to write a QList into a binary file?
try this example
h - file
Code:
class Foo
{
public:
: m_number(number), m_name(name)
{}
void setNumber(int number) { m_number = number; }
int number() const { return m_number; }
void setName
(const QString &name
) { m_name
= name;
} QString name
() const { return m_name;
}
private:
int m_number;
};
cpp-file
Code:
return;
QList<Foo> res;
for (int i = 0; i < 11; ++i) {
Foo f(i, tr("name%1").arg(i));
res << f;
}
out << res;
file.close();
return;
res.clear();
in >> res;
for (int i = 0; i < res.size(); ++i) {
const Foo f = res.at(i);
qDebug() << f.number() << " " << f.name();
}
Re: How to write a QList into a binary file?
Thank you JhonJames and spirit, writing and reading the List are working!
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
Code:
static QList<Server> serversList;
to a binary file
I my server.cpp i have the following lines of code:
Code:
QDataStream& operator<<(QDataStream& out, const Server& server)
{
out << server.serverAddres;
...
out << server.serverUseSsl;
return out;
}
QDataStream& operator>>(QDataStream& in,Server& server)
{
in >> server.serverAddres;
...
in >> server.serverUseSsl;
return in;
}
In the file serverlist.cpp i do the operation:
Code:
if(!serverDataFile.
open(QIODevice::ReadWrite)) {
return;
}
outputStream << listofServers;
but when i compile i get the following error;
Code:
O:/projects/Post_Program/serverlist.cpp:44: error: no match for 'operator<<' in '((serverList*)this)->serverList::outputStream << serverList::listofServers'
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:
Code:
(*outputStream) << listofServers
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:
Code:
f:/Qt/2009.02/qt/include/QtCore/../../src/corelib/io/qdatastream.h: In function 'QDataStream& operator<<(QDataStream&, const QList<T>&) [with T = Server]':
serverlist.cpp:45: instantiated from here
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)'
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:
Quote:
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.
Re: How to write a QList into a binary file?
Server consits of a few QString's some bool's and qint32.
Re: How to write a QList into a binary file?
Would you post the full code of the serverlist.cpp file
Re: How to write a QList into a binary file?
This is the serverlist.cpp:
Code:
#include <QDir>
#include <QMessageBox>
#include "serverlist.h"
QList<Server> listofServers;
serverList::serverList()
{
settings = new Settings();
QFile serverDataFile
(settings
->getappDataDir
() + QDir::separator() + "servers" + QDir::separator() + "servers.dat");
serverDataFileInfo
= new QFileInfo(serverDataFile
);
}
void serverList::saveServers()
{
if(serverDataFileInfo->exists())
{
QDir newFile
(serverDataFileInfo
->absoluteFilePath
());
newFile.mkpath(serverDataFileInfo->absolutePath());
}
if(!serverDataFile.
open(QIODevice::ReadWrite)) {
QMessageBox::critical( 0,
"Post program",
"There was a problem reading the server settings file" );
}
outputStream << listofServers;
}
void serverList::addServer(Server newServer)
{
}
This is the serverlist.h:
Code:
#ifndef SERVERLIST_H
#define SERVERLIST_H
#include <QList>
#include <QFile>
#include <QFileInfo>
#include <QDataStream>
#include <server.h>
#include <settings.h>
class serverList
{
public:
serverList();
void saveServers();
void addServer(Server newServer);
void loadServers();
private:
static QList<Server> listofServers;
Settings *settings;
};
#endif // SERVERLIST_H
I will post the Server class also to get the complete picture
server.cpp:
Code:
#include "server.h"
//#include <QDataStream>
Server::Server()
{
}
void Server
::setAddres(QString newAddres
) {
serverAddres = newAddres;
}
void Server
::setNickname(QString newNickname
) {
serverNickname = newNickname;
}
void Server::setPort(qint32 newPort)
{
serverPort = newPort;
}
void Server::setConnections(qint32 newConnections)
{
serverConnections = newConnections;
}
void Server
::setUsername(QString newUsername
) {
serverUsername = newUsername;
}
void Server
::setPassword(QString newPassword
) {
serverPassword = newPassword;
}
void Server::setRequireLogin(bool newRequireLogin)
{
serverRequireLogin = newRequireLogin;
}
void Server::setUseSsl(bool newUseSsl)
{
serverUseSsl = newUseSsl;
}
{
return serverAddres;
}
{
return serverNickname;
}
qint32 Server::getPort()
{
return serverPort;
}
qint32 Server::getConnections()
{
return serverConnections;
}
{
return serverUsername;
}
{
return serverPassword;
}
bool Server::getRequireLogin()
{
return serverRequireLogin;
}
bool Server::getUseSsl()
{
return serverUseSsl;
}
QDataStream& operator<<(QDataStream& out, const Server& server)
{
out << server.serverAddres;
out << server.serverNickname;
out << server.serverPort;
out << server.serverConnections;
out << server.serverUsername;
out << server.serverPassword;
out << server.serverRequireLogin;
out << server.serverUseSsl;
return out;
}
QDataStream& operator>>(QDataStream& in,Server& server)
{
in >> server.serverAddres;
in >> server.serverNickname;
in >> server.serverPort;
in >> server.serverConnections;
in >> server.serverUsername;
in >> server.serverPassword;
in >> server.serverRequireLogin;
in >> server.serverUseSsl;
return in;
}
server.h
Code:
#ifndef SERVER_H
#define SERVER_H
#include <QString>
#include <QtGlobal>
class Server
{
public:
Server();
//Set functions
void setNickname
(QString newNickname
);
void setPort(qint32 newPort);
void setConnections(qint32 newConnections);
void setUsername
(QString newUsername
);
void setPassword
(QString newPassword
);
void setRequireLogin(bool newRequireLogin);
void setUseSsl(bool newUseSsl);
//Get functions
qint32 getPort();
qint32 getConnections();
bool getRequireLogin();
bool getUseSsl();
private:
qint32 serverPort;
qint32 serverConnections;
bool serverRequireLogin;
bool serverUseSsl;
};
#endif // SERVER_H
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:
Code:
QDataStream& operator<<(QDataStream& out, const Server& server);
QDataStream& operator>>(QDataStream& in,Server& server);
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.
Code:
O:/projects/Post_Program/server.cpp:107: error: no match for 'operator>>' in 'in >> server->Server::serverPort'
Re: How to write a QList into a binary file?
Wait a minute, aren't those members private? how are you accessing them directly?