这里摘取其关键内容:
1、将自定义数据类型使用Q_DECLARE_METATYPE宏进行声明,便于编译器识别。
2、在插入对象的时候,声明QVariant对象,使用其setValue方法添加自定义对象。
3、在读取的时候,采用QVariant对象的value方法返回自定义对象。
写入:
- //类型转换
- QVariant v;
- v.setValue(MyStruct);
读取: - //转换为自定义的结构体实例
- MyStruct s = v.value<MyStruct>();
参考了博文后写了自己的结构体,但是在用qsettings读出某一条记录时提示错误:
QVariant::load: unable to load type 1055.
同时,在写qsettings,会提示如下错误:
QVariant::save: unable to save type ‘DownloadFinishItemInfo‘ (type id: 1055).(其中DownloadFinishItemInfo为自定义结构体)
针对以上问题,加入如下补充后问题得到解决:
1、在用qsettings读出某一条记录前需增加qRegisterMetaTypeStreamOperators的声明;
2、需要重载关于自定义结构体的输入和输出操作符。
个人猜测:对于qsettings要保存自定义的结构体,其实就是保存到硬盘的配置文件中,所以需要对这个结构体有读出和写入的api,这里是重载输入和输出操作符,这样,在用qsettings保存一条记录的时候,就用该结构体的输出操作符把结构体内容读出到流,然后把流中的内容保存到文件,同样,在用qsettings读出一条记录时,就需要用到该结构体的输出操作符,把结构体内容输出到流,然后我们才能从流中读到该结构体的内容。
上代码:
.h:
/*已下载的任务的信息*/
#ifndef DOWNLOADFINISHITEMINFO_H
#define DOWNLOADFINISHITEMINFO_H#include <QMetaType>
#include <QString>
#include <QDataStream>class DownloadFinishItemInfo
{
public:
DownloadFinishItemInfo();QString downloadTime; //下载时间
QString softwareName; //软件名
QString softwareIconPath;//图标路径
QString softwareTotalSizeStr; //总大小的字符串表示
};Q_DECLARE_METATYPE(DownloadFinishItemInfo) //将自定义数据类型使用Q_DECLARE_METATYPE宏进行声明,便于编译器识别
QDataStream & operator<<(QDataStream &out, const DownloadFinishItemInfo info);
QDataStream & operator>>(QDataStream &out, DownloadFinishItemInfo &info);#endif // DOWNLOADFINISHITEMINFO_H
.cpp:
#include "downloadfinishiteminfo.h"
#include <QMetaType>
#include <QDebug>DownloadFinishItemInfo::DownloadFinishItemInfo()
{
}QDataStream & operator<<(QDataStream &out, const DownloadFinishItemInfo info) //定义怎么把自定义结构体的数据写入一条qsettings
{out << info.downloadTime.toStdString().c_str() \
<< info.softwareIconPath.toStdString().c_str() \
<< info.softwareName.toStdString().c_str() \
<< info.softwareTotalSizeStr.toStdString().c_str();
// qDebug() << "*****************" << info.downloadTime.toStdString().c_str() \
// << info.softwareIconPath.toStdString().c_str() \
// << info.softwareName.toStdString().c_str() \
// << info.softwareTotalSizeStr.toStdString().c_str();return out;
}QDataStream & operator>>(QDataStream &out, DownloadFinishItemInfo& info) //定义怎么把从qsettings读出的一条数据保存到自定义结构体中
{
char *str1= new char(100);
char *str2= new char(100);
char *str3= new char(100);
char *str4= new char(100);
out >> str1;
//qDebug() << "@@@@@@@@" <<str1;
info.downloadTime.toStdString().clear();
info.downloadTime.append(str1);out >> str2;
//qDebug() << "@@@@@@@@" <<str2;
info.softwareIconPath.toStdString().clear();
info.softwareIconPath.append(str2);out >> str3;
//qDebug() << "@@@@@@@@" <<str3;
info.softwareName.toStdString().clear();
info.softwareName.append(str3);out >> str4;
//qDebug() << "@@@@@@@@" <<str4;
info.softwareTotalSizeStr.toStdString().clear();
info.softwareTotalSizeStr.append(str4);
//qDebug() << "&&&&&&&&&" <<str;
//out >>&(info.downloadTime);
delete str1;
delete str2;
delete str3;
delete str4;
return out;
} - 读取和写入setting:
- void DownloadProWidget::WriteDownloadFinishedTask(DownloadFinishItemInfo info)
{
//写进配置文件(保存3天内的已下载任务)
QSettings *pSettings = new QSettings("downloadFinished.ini", QSettings::IniFormat, this);pSettings->beginGroup("Withinthreedays");
QVariant variant;DownloadFinishItemInfo info1;
info = info1;
variant.setValue(info);
pSettings->setValue(info.softwareName, variant);
pSettings->endGroup();}
QList<DownloadFinishItemInfo> DownloadProWidget::ReadDownloadFinishedTask()
{
qRegisterMetaTypeStreamOperators<DownloadFinishItemInfo>("DownloadFinishItemInfo"); //关键,不然读出来的都是空//读取配置文件(3天内的已下载任务)
QSettings *pSettings = new QSettings("downloadFinished.ini", QSettings::IniFormat, this);//读取所有key
QStringList keyList = pSettings->allKeys();QList<DownloadFinishItemInfo> infoList;
//保存所有信息
int length = keyList.length();
for(int i = 0; i < length; i++)
{
QVariant variant = pSettings->value(keyList.at(i));
DownloadFinishItemInfo info = variant.value<DownloadFinishItemInfo>();
infoList.append(info);}
return infoList;
}
http://blog.csdn.net/u013281495/article/details/50970725