QSettings: Move / change the scope / location of an existing QSettings Object

809 views Asked by At

I am writing a program which stores all its settings in a QSettings object. Now I would like to give the user the option to change the storage location of his data at any time. Apparantly QSettings does not provide the ability to change its scope and copy all its data to a new location, say from the registry to a file in %APPDATA%.

What I know:

  • QSettings::setPath() is used before construction and doesn't affect existing objects.
  • The copy operator is private. I could subclass QSettings but I am afraid of settings being lost during the copy operation because of other threads writing simultaneously.

How do I move and retain my program settings on the fly to a new location? I'd really like to achieve this with QSettings if possible.

1

There are 1 answers

0
pokey909 On

Would a thread-safe singleton wrapper class be an option?

class Settings {

public:
    static Settings& instance() 
    { 
        static Settings* inst = 0;
        if (!inst)
            inst = new Settings();
        return *inst; 
    }
    QSettings& getSettings { QMutexLocker(&m_mutex); return *m_settings; }
    bool migrateLocation(...) 
    { 
        QMutexLocker(&m_mutex); 
        QSettings* newSettings = new QSettings(...new parameters...);
        //... copy over the stuff
        delete m_settings;
        m_settings = newSettings;
    }
private:
    Settings() { m_settings = new QSettings(...); }

    static QMutex m_mutex;
    QSettings* m_settings;
}