Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save (already-existing) QSetting into an INI file

Tags:

qt

ini

qsettings

I want to save an alredy-existing QSettings object into some INI file for backup.

The QSettings comes from the application's global settings, ie. it can be registry, ini file, etc.


In case it helps, my context is:

class Params
{
    // All params as data members
    // ...
    void loadGlobal ()
    {
        Qettings s; // Global parameters, paths set by application
        // Fill data members: s.value (...);
    }
};

class Algo
{
    Result run (Params p)
    {
        Result r = F(p);
        return r;
    }
};

int main (...)
{
    Params p;
    p.loadGlobal ();
    Algo a;
    Result r = a.run (p);

    // At this point, save Result and Params into a specific directory
    // Is there a way to do:
    p.saveToIni ("myparams.ini"); // <-- WRONG
}

A solution would be to add a saveTo (QSetting & s) method into the Params class:

class Params
{
    void saveTo (QSettings & s)
    {
        s.setValue (...);
    }
};

int main (...)
{
    Params p;
    p.loadGlobal ();
    QSettings bak ("myparams.ini", ...);
    p.saveTo (bak);
}

But I am looking for a solution without modifying the Params class.

like image 516
Julien-L Avatar asked Jan 19 '12 17:01

Julien-L


1 Answers

Well, no, QT Doesn't really support this directly. I think your best bet is writing a helper class...something like:

void copySettings( QSettings &dst, QSettings &src )
{
    QStringList keys = src.allKeys();
    for( QStringList::iterator i = keys.begin(); i != keys.end(); i++ )
    {
        dst.setValue( *i, src.value( *i ) );
    }
}
like image 50
Mike Buland Avatar answered Oct 27 '22 21:10

Mike Buland