我有一个从QSetting读取自定义元类型数据的问题。我有一个类:
class MusicOwner
{
public:
MusicOwner() :
songs_count(0),
id(0)
{}
explicit MusicOwner(const Song &owner_radio);
Song toOwnerRadio() const;
static QList<MusicOwner> parseMusicOwnerList(const QVariant &request_result);
private:
friend QDataStream &operator <<(QDataStream &stream, const VkService::MusicOwner &val);
friend QDataStream &operator >>(QDataStream &stream, VkService::MusicOwner &val);
friend QDebug operator<< (QDebug d, const MusicOwner &owner);
int songs_count;
int id;
QString name;
QString screen_name;
QUrl photo;
};
Q_DECLARE_METATYPE(VkService::MusicOwner)
与重载:QDataStream &operator <<(QDataStream &stream, const VkService::MusicOwner &val)
{
stream << val.id;
stream << val.name;
stream << val.songs_count;
stream << val.screen_name;
return stream;
}
QDataStream &operator >>(QDataStream &stream, VkService::MusicOwner &val)
{
stream >> val.id;
stream >> val.name;
stream >> val.songs_count;
stream >> val.screen_name;
return stream;
}
QDebug operator <<(QDebug d, const VkService::MusicOwner &owner)
{
d << "MusicOwner("
<< owner.id << ","
<< owner.name << ","
<< owner.songs_count << ","
<< owner.screen_name << ")";
return d;
}
在程序开始的某处我调用:
qRegisterMetaTypeStreamOperators<MusicOwner>("MusicOwner");
要读和写,我使用两个函数:保存工作正常,我保存的所有数据都出现在设置文件中。
void VkService::SaveBookmarks()
{
TRACE;
QSettings s;
s.beginGroup(kSettingGroup);
s.beginWriteArray("bookmarks");
int index = 0;
for (int i = 0; i < root_item_->rowCount(); ++i){
auto item = root_item_->child(i);
if (item->data(InternetModel::Role_Type).toInt() == Type_Bookmark){
Song song = item->data(InternetModel::Role_SongMetadata).value<Song>();
s.setArrayIndex(index);
MusicOwner owner(song);
qLog(Info) << "Save" << index << ":" << owner;
s.setValue("owner", QVariant::fromValue(owner));
++index;
}
}
s.endArray();
}
这个函数的问题,加载计数正确,但是加载的是空的默认构造项
void VkService::LoadBookmarks()
{
QSettings s;
s.beginGroup(kSettingGroup);
int max = s.beginReadArray("bookmarks");
for (int i = 0; i < max; ++i){
s.setArrayIndex(i);
MusicOwner owner = s.value("owner").value<MusicOwner>();
qLog(Info) << "Load" << i << ":" << owner;
AppendBookmarkFromRadio(root_item_, owner.toOwnerRadio());
}
s.endArray();
}
我将它重写到新项目中进行测试,但它工作良好。我花了两个小时来找出,为什么这个变体正确读取数据而第一个变体不正确。第一个变量的所有者正确转换。即使两个变体的设置文件中的数据相同。
#include <QCoreApplication>
#include <QDataStream>
#include <QDebug>
#include <QUrl>
#include <QSettings>
#include <QVariant>
class MusicOwner
{
public:
MusicOwner() :
songs_count(0),
id(0)
{}
private:
friend QDataStream &operator <<(QDataStream &stream, const MusicOwner &val);
friend QDataStream &operator >>(QDataStream &stream, MusicOwner &val);
friend QDebug operator<< (QDebug d, const MusicOwner &owner);
public:
int songs_count;
int id;
QString name;
QString screen_name;
QUrl photo;
};
QDataStream &operator <<(QDataStream &stream, const MusicOwner &val)
{
stream << val.id;
stream << val.name;
stream << val.songs_count;
stream << val.screen_name;
return stream;
}
QDataStream &operator >>(QDataStream &stream, MusicOwner &val)
{
stream >> val.id;
stream >> val.name;
stream >> val.songs_count;
stream >> val.screen_name;
return stream;
}
QDebug operator <<(QDebug d, const MusicOwner &owner)
{
d << "MusicOwner("
<< owner.id << ","
<< owner.name << ","
<< owner.songs_count << ","
<< owner.screen_name << ")";
return d;
}
Q_DECLARE_METATYPE(MusicOwner)
const QString kSettingGroup = "Group";
void Save() {
QSettings s;
s.beginGroup(kSettingGroup);
s.beginWriteArray("bookmarks");
int index = 0;
for (int i = 0; i < 100; ++i){
if (random() % 5 == 0) {
s.setArrayIndex(index);
MusicOwner owner;
owner.id = i;
owner.name ="Hello world";
owner.songs_count = i * 2;
owner.screen_name = "hello_world";
s.setValue("owner", QVariant::fromValue(owner));
qDebug() << "Saved" << i << ":" << owner;
++index;
}
}
s.endArray();
qDebug() << "Saved" << index << "elements";
}
void Load() {
QSettings s;
s.beginGroup(kSettingGroup);
int max = s.beginReadArray("bookmarks");
qDebug() << "To load" << max << "elements";
for (int i = 0; i < max; ++i){
s.setArrayIndex(i);
MusicOwner owner = s.value("owner").value<MusicOwner>();
qDebug() << "tLoaded" << i << ":" << owner;
}
s.endArray();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qRegisterMetaTypeStreamOperators<MusicOwner>("MusicOwner");
QSettings s;
Load();
Save();
return a.exec();
}
也许你可以在这些变体中找到与QSettings工作的差异?
在这个示例中,您对名称空间一点也不小心。如果您在非工作代码中使用了名称空间,那么您就不能做出不使用名称空间的"最小情况"。
下面的可编译示例使用名称空间,并且在Qt 4.8和5.1中都工作良好。请注意,这些设置在4.8和5.1之间默认是不可移植的,我不知道这是一个bug还是一个特性。
#include <QCoreApplication>
#include <QDataStream>
#include <QDebug>
#include <QSettings>
#include <QVariant>
namespace VkService {
class MusicOwnerA
{
friend QDataStream &operator <<(QDataStream &stream, const MusicOwnerA &val);
friend QDataStream &operator >>(QDataStream &stream, MusicOwnerA &val);
friend QDebug operator<< (QDebug d, const MusicOwnerA &owner);
public:
MusicOwnerA() : id(0) {}
int id;
QString name;
};
QDataStream &operator <<(QDataStream &stream, const VkService::MusicOwnerA &val)
{
stream << val.id;
stream << val.name;
return stream;
}
QDataStream &operator >>(QDataStream &stream, VkService::MusicOwnerA &val)
{
stream >> val.id;
stream >> val.name;
return stream;
}
QDebug operator <<(QDebug d, const VkService::MusicOwnerA &owner)
{
d << "VkService::MusicOwnerA("
<< owner.id << ","
<< owner.name << ")";
return d;
}
}
Q_DECLARE_METATYPE(VkService::MusicOwnerA)
void Save() {
QSettings s;
s.beginWriteArray("bookmarks");
int index = 0;
for (int i = 0; i < 100; ++i){
if (random() % 5 == 0) {
s.setArrayIndex(index);
VkService::MusicOwnerA owner;
owner.id = i;
owner.name ="Hello world";
s.setValue("owner", QVariant::fromValue(owner));
qDebug() << "Saved" << i << ":" << owner;
++index;
}
}
s.endArray();
qDebug() << "Saved" << index << "elements";
}
void Load() {
QSettings s;
int max = s.beginReadArray("bookmarks");
qDebug() << "To load" << max << "elements";
for (int i = 0; i < max; ++i){
s.setArrayIndex(i);
VkService::MusicOwnerA owner = s.value("owner").value<VkService::MusicOwnerA>();
qDebug() << "tLoaded" << i << ":" << owner;
}
s.endArray();
}
int main(int argc, char **argv)
{
QCoreApplication a(argc, argv);
a.setOrganizationDomain("16549302.stackoverflow.com");
qRegisterMetaTypeStreamOperators<VkService::MusicOwnerA>("VkService::MusicOwnerA");
Load();
Save();
}