PDA

View Full Version : How to save application settings which can be accessible in all forms?



tofighi
16th July 2011, 04:42
I'm creating an application which should have Options form (dialog) and in this form I want to save some parameters such as some folder path, some configuration numbers,etc.
I want after saving the setting in the Options dialog, it saves it in the QSettings object and I'll be able to reload it when I need it.
How can I do this? and because these settings are important to all of other dialogs, I want to know the most efficient way to save these settings and also load it easily and also it will be accessible to all dialogs.
For example I have three forms:
MainWindow
Options
SomeDialog

folderPath1 = "c:/windows";
folderPath2 = "d:/programs";
size = 100;

I want to load Settings (folderPath1 , folderPath2 , size) in the start of program using MainWindow. Then I would like to be able to edit these settings via Options dialog.
In every point of program I would like to have access to these settings from SomeDialog.

Thanks in advance.

ChrisW67
16th July 2011, 06:56
QSettings, which you already identified, is a solution to this problem. What exactly is your difficulty?

kosasker
16th July 2011, 07:53
All about logic. You can find help about QSettings (http://doc.qt.nokia.com/latest/qsettings.html)

you can write a function that loads your setting from an ini file. And call it from main form constructor.

For a ini file;


QString myinifilepath("myprogsettings.ini");
QSettings myini(myinifilepath,QSettings::IniFormat);
myini.beginGroup("Settings");
if (myini.value("folderPath1").isValid())
{
ui->lineEdit1->setText(myini.value("folderPath1").toString());
}
.....
.....
....
myini.endGroup();


if you want to save your settings, you can call a save function, which event you want.




myini.setValue("folderPath1",ui->lineEdit1->text());



please read the docs and write little prototype applications to learn.