从QSettings中读取自定义元类型的数组

时间:2013-05-14 17:29:04

标签: c++ qt qvariant qsettings

我在从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一起工作的不同之处?

1 个答案:

答案 0 :(得分:0)

在示例中,您对命名空间一点都不小心。如果您确实在非工作代码中使用命名空间,则不能创建不使用命名空间的“最小情况”。

以下可编译示例使用命名空间,并在Qt 4.8和5.1中都正常工作。请注意,默认情况下,这些设置在4.8和5.1之间不可移植,我不知道这是错误还是功能。

#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();
}