Can't write a Qlist to a file using QDataStream

2.2k Views Asked by At

I am trying to develop a Qt App using 4.7.3 which involves the writing of a QList to a flie.

My class is:

class Task  
{  
public:  
    QString ta, desc;  
    QTime ti;  
    QDate da; 
    int pri, diff;  
    bool ala;  
};  

the corresponding QList is : QList tasks;

My file is :

QFile theFile("dataBase");  
QDataStream stream(&theFile);  
stream.setVersion(QDataStream::Qt_4_7);

to read:

theFile.open(QIODevice::ReadOnly);  
stream >> tasks;

to write:

theFile.open(QIODevice::WriteOnly);  
stream << tasks;  

while compiling on Windows using Qt 4.7.3 and GCC 4.4 toolchain I get the following error:

c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtCore\qstringlist.h:46: In file included from c:/QtSDK/Desktop/Qt/4.7.3/mingw/include/QtCore/qstringlist.h:46,

c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtCore\qdatastream.h:250: error: no match for 'operator>>' in 's >> t'

c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtCore\qdatastream.h:-1: In function 'QDataStream& operator<<(QDataStream&, const QList&) [with T = Task]':

c:\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtCore\qdatastream.h:263: error: no match for 'operator<<' in 's << ((const QList*)l)->QList::at with T = Task'

Although the overload for the << and >> operators exist, I cant find any reason for these errors.....

Please Help Anyone, as this is a pretty important app which I have to make
Thanks in Advance....

1

There are 1 best solutions below

0
On BEST ANSWER

You need to define you own operator out and in ... Something like this:

.h file:

class myClass{
public:
    QString name;
    QString gender;
    QDate birthDay;
    QString job;
    QString address;
    int phoneNo;
};

QDataStream &operator <<(QDataStream &stream, const myClass &myclass);
QDataStream &operator >>(QDataStream &stream, myClass &myclass);

.cpp file:

QDataStream &operator <<(QDataStream &stream, const myClass &myclass)
{
    stream<<myclass.address;
    stream<<myclass.birthDay;
    stream<<myclass.gender;
    stream<<myclass.job;
    stream<<myclass.name;
    stream<<myclass.phoneNo;
    return stream;
}

QDataStream &operator >>(QDataStream &stream, myClass &myclass)
{
    stream>>myclass.address;
    stream>>myclass.birthDay;
    stream>>myclass.gender;
    stream>>myclass.job;
    stream>>myclass.name;
    stream>>myclass.phoneNo;
    return stream;
}