it depends which type you use in template.
it depends which type you use in template.
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
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:To copy to clipboard, switch view to plain text mode
implement your own QDataStrem:: operator << & >> for this type and everything should work.
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
Lawand (31st March 2009)
You mean something like this:
Qt Code:
{ *this >> patient.firstName; *this >> patient.lastName; return *this; } { *this << patient.firstName; *this << patient.lastName; return *this; }To copy to clipboard, switch view to plain text mode
almost, I would use these signatures.
Qt Code:
{ ... } { ... }To copy to clipboard, switch view to plain text mode
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
Carlton (30th July 2009)
Please bear with me ,
I defined those in the patient.cpp file:
Qt Code:
{ in << patient.firstName; in << patient.lastName; return in; } { out >> patient.firstName; out >> patient.lastName; return out; }To copy to clipboard, switch view to plain text mode
but I got these errors at the opening curly brace of operator<<:
Qt Code:
Multiple markers at this line - non-inline function 'QDataStream& QDataStream::operator<<(QDataStream&, const Patient&)' is defined after prior declaration as dllimport: attribute ignored does not match any in class `QDataStream' exactly one argumentTo copy to clipboard, switch view to plain text mode
and these errors at the opening curly brace of operator>>:
what's wrong?Qt Code:
Multiple markers at this line exactly one argument - prototype for `QDataStream& QDataStream::operator>>(QDataStream&, Patient&)' does not match any in class `QDataStream'To copy to clipboard, switch view to plain text mode
Here:
Qt Code:
//spirit's code: //Your code: ^^^^^^^^^^^^^To copy to clipboard, switch view to plain text mode
Lawand (31st March 2009)
try this example
h - file
Qt Code:
class Foo { public: : m_number(number), m_name(name) {} void setNumber(int number) { m_number = number; } int number() const { return m_number; } private: int m_number; QString m_name; }; To copy to clipboard, switch view to plain text mode
cpp-file
Qt 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(); }To copy to clipboard, switch view to plain text mode
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
Lawand (31st March 2009)
Thank you JhonJames and spirit, writing and reading the List are working!
Last edited by Lawand; 31st March 2009 at 19:50.
I have a similar kind of problem. I want to write a QList with custom classes to a binary file.
I want to write ato a binary fileQt Code:
static QList<Server> serversList;To copy to clipboard, switch view to plain text mode
I my server.cpp i have the following lines of code:
Qt 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; }To copy to clipboard, switch view to plain text mode
In the file serverlist.cpp i do the operation:
Qt Code:
{ return; } outputStream << listofServers;To copy to clipboard, switch view to plain text mode
but when i compile i get the following error;
Qt Code:
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
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:
(*outputStream) << listofServersTo copy to clipboard, switch view to plain text mode
eekhoorn12 (13th June 2009)
Thanks that solved the problem but created a new one.
I now get the following error:
Qt 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)'To copy to clipboard, switch view to plain text mode
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.
Bookmarks