2016-04-22 16 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; 
} 
関連する問題