无法使用QDataStream将Qlist写入文件

时间:2012-01-14 16:13:48

标签: qt operator-overloading qlist

我正在尝试使用4.7.3开发一个Qt App,其中包括将QList写入flie。

我的课程是:

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

对应的QList是:QList任务;

我的档案是:

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

阅读:

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

写:

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

在使用Qt 4.7.3和GCC 4.4工具链在Windows上进行编译时出现以下错误:

c:\ QtSDK \ Desktop \ Qt \ 4.7.3 \ mingw \ include \ QtCore \ qstringlist.h:46:包含在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:错误:不匹配'运算符&gt;&gt;'在's&gt;&gt; t'

c:\ QtSDK \ Desktop \ Qt \ 4.7.3 \ mingw \ include \ QtCore \ qdatastream.h:-1:在函数'QDataStream&amp;运算符&lt;&lt;(QDataStream&amp;,const QList&amp;)[with T = Task]':

c:\ QtSDK \ Desktop \ Qt \ 4.7.3 \ mingw \ include \ QtCore \ qdatastream.h:263:错误:不匹配'operator&lt;&lt;'在's&lt;&lt; ((const QList *)l) - &gt; QList :: at with T = Task'

虽然&lt;&lt;&lt;和&gt;&gt;运营商存在,我找不到这些错误的原因.....

请帮助任何人,因为这是一个非常重要的应用程序,我必须做 在此先感谢....

1 个答案:

答案 0 :(得分:3)

你需要定义自己的操作员,然后进入......这样的事情:

.h文件:

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文件:

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