The simplest way is using existing tools. QList has I/O operators for QDataStream. You need to equip your test class by the corresponding I/O operators:
friend QDataStream& operator << ( QDataStream& out, const test& ob )
{
output ob to out;
return out;
}
friend QDataStream& operator >> ( QDataStream& in, test& ob )
{
read data from in and and initialize ob;
return in;
}
friend QDataStream& operator << ( QDataStream& out, const test& ob )
{
output ob to out;
return out;
}
friend QDataStream& operator >> ( QDataStream& in, test& ob )
{
read data from in and and initialize ob;
return in;
}
To copy to clipboard, switch view to plain text mode
Now, open a file (QFile), associate it with a QDataStream (say, fileio) and do fileio << list; or fileio >> list. The QList class will iterate through its contents and outputs stored items to the file. Or, it will append items stored in the file to the current contents of the list. Therefore, before reading in, do list.clear(). The output is a binary file.
Bookmarks