使用QT设置获取配置文件设置 [英] Getting the profile setting using QT settings

查看:317
本文介绍了使用QT设置获取配置文件设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Qt设置,它将对象保存到文件中。它保存到一个名为sessionrc的文件。

I'm using the Qt settings and it saves the object into a file. it saves to a file called sessionrc.

现在我正在尝试从设置加载对象并将其保存。

Now I'm trying to load the object from the settings and save it back.

问题是我无法从设置中识别对象,所以我可以加载保存的所有配置文件。

The problem is I can not identify the object from the settings, so that I can load all the profiles that are saved.

我正在使用以下加载和保存功能

I'm using the following load and save functionality

void ProfileManager::loadFrom(Settings &set, bool ownGroup)
{
    qDebug()<<"LOAD";
    foreach (const QString &group, set.childGroups()) {
        if(group == "Profile")
        {
            Profile *profile = new Profile();
            profile->setObjectName(group);
            profile->loadFrom(set);
            m_Profiles << profile;
        }
    }


    EraObject::staticLoadFrom(set, this);

}

void ProfileManager::saveTo(Settings &set, bool ownGroup, bool force)
{
    EraObject::staticSaveTo(set, this, ownGroup, force);

     foreach(Profile * profile, m_Profiles) {
        profile->saveTo(set);
    }

}

当前设置文件



The current setting file is

[www]
Ta=20
Te=48
Texp=38
lim1=0
lim2=0
offset=0
profilename=www

[www]是保存的配置文件。但我有很多。如何将其加载并正确保存

[www] is the profile that is saved. but I have many of it. How would I load it back and save it correctly

推荐答案

// main.cpp
#include <QCoreApplication>
#include <QSettings>
#include <QVector>
#include <QDebug>
#include <QMetaProperty>

class Profile : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName )
    Q_PROPERTY(QString title READ title WRITE setTitle )

public:
    explicit Profile(QObject *parent = 0) : QObject(parent) {
    }

    QString name() const {
        return name_;
    }
    void setName(QString name) {
        name_ = name;
    }

    QString title() const {
        return title_;
    }
    void setTitle(QString title) {
        title_ = title;
    }

    void save(QSettings& settings) const {
        for(int i=0; i<metaObject()->propertyCount(); ++i) {
            const auto& p = metaObject()->property(i);
            if(p.isStored(this)) {
                settings.setValue(p.name(), property(p.name()));
            }
        }
    }

    void load(QSettings& settings) {
        for(int i=0; i<metaObject()->propertyCount(); ++i) {
            const auto& p = metaObject()->property(i);
            if(p.isStored(this)) {
                setProperty(p.name(), settings.value(p.name()));
            }
        }
    }

private:
    QString name_;
    QString title_;
};

#include "main.moc"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QObject garbageCollector;
    QVector<Profile*> profiles;
    {
        Profile* p1 = new Profile(&garbageCollector);
        p1->setName("profilename1");
        p1->setTitle("Profile 1");
        Profile* p2 = new Profile(&garbageCollector);
        p2->setName("profilename2");
        p2->setTitle("Profile 2");
        profiles.append(p1);
        profiles.append(p2);
    }

    QSettings s("profiles.ini", QSettings::IniFormat);

    // write profiles
    {
        s.beginGroup("profiles");
        foreach(const Profile*p, profiles) {
            s.beginGroup(p->name());
            p->save(s);
            s.endGroup();
        }
        s.endGroup();
        s.sync(); // force write
    }
    // read profiles
    {
        s.beginGroup("profiles");
        foreach(const QString& g, s.childGroups()) {
            Profile p;
            s.beginGroup(g);
            p.load(s);
            s.endGroup();
            qDebug() << p.name();
            qDebug() << p.title();
        }
        s.endGroup();

    }
    return 0;
}

这篇关于使用QT设置获取配置文件设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆