2016-04-22 84 views
0

我正在使用Qt設置並將對象保存到文件中。它保存到一個名爲sessionrc的文件中。使用QT設置獲取配置文件設置

現在我試圖從設置中加載對象並保存。

問題是我無法從設置中識別對象,以便我可以加載保存的所有配置文件。

我使用下面的加載和保存功能

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

} 

當前的設置文件是

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

[WWW]是保存配置文件。但我有很多。我如何將其加載並正確保存

回答

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