PDA

View Full Version : How to use QSettings to read INI file



Cantora
3rd June 2009, 03:28
Hi All,

I am trying to use QSettings to read the INI file but I have try few times but still fail to read it :(


The format for INI file is like below:

[System.Setting]
auto_check_update=false
auto_check_mode=weekly

Thanks

Cantora
3rd June 2009, 03:35
Found out the way :)

QString strKey("System.Setting/");
QSettings * settings = 0;
settings = new QSettings( m_strWorkingPath, QSettings::IniFormat );
ret = settings->value( strKey + "auto_check_update", "r").toBool();
strMode = settings->value( strKey + "auto_check_mode", "r").toString();

Lykurg
3rd June 2009, 07:21
QString strKey("System.Setting/");

Better use QSettings::beginGroup() and QSettings::endGroup().

candyshop
28th October 2009, 01:20
Hi, for me
The format for INI file is like below:

[players]
player0name=mike
player0surname=jane
player1name=scootie
...
...

I am trying to use QSettings to read the INI file..
I tried this:
settings = new QSettings("config.ini",QSettings::IniFormat);
settings->beginGroup("players");
int size = settings->beginReadArray("player");
ui->lineEdit_2->setText(QString::number(size)); // this is value-test

for (int index = 0; index < size; ++index)
{
settings->setArrayIndex(index);
ui->choosePlayerBox->addItem(settings->value("name").toString());
}

settings->endArray();
settings->endGroup();

But it doesn`t work..
I tried also:
[players]
player/0/name=mike
player/0/surname=jane
player/1/name=scootie
...
...
But it also doesn`t work..
Any ideas ?
Thanks

spirit
28th October 2009, 08:00
try this example:


QSettings settings("test.ini", QSettings::IniFormat);
settings.beginGroup("players");
const QStringList childKeys = settings.childKeys();
foreach (const QString &childKey, childKeys)
qDebug() << settings.value(childKey);
settings.endGroup();

candyshop
28th October 2009, 11:18
ok, i have ini file like:
[user]
0/name=lukasz
0/wins=3
0/loses=3
1/name=pawel
1/wins=1
1/loses=5
2/name=anna
2/wins=1
2/loses=5

and my code is:



settings = new QSettings("config.ini",QSettings::IniFormat);
settings->beginGroup("user");
QStringList player_number = settings->childGroups(); // returns 0, 1, 2 - OK !
const QStringList childKeys = settings->childKeys(); // should return name, wins, ... right ?
foreach(const QString &childKey, childKeys)
{
ui->choosePlayerBox->addItem(settings->value(childKey).toString()); // should add lukasz, 3, 3, pawel...., but it doesn`t work

}
settings->endGroup();


Any ideas ?

spirit
28th October 2009, 11:23
you code will not work, because "0/name" it's a key, not a group.
I suggest you to review format on your ini-file or use xml instead.

meena
2nd June 2010, 08:02
Hi
my .ini file is

[System.Setting]
auto_check_update=false
auto_check_mode=weekly

& the code to read is
QString strKey("System.Setting/");
QSettings * settings = 0;
settings = new QSettings( file_path, QSettings::IniFormat );
QString strMode = settings->value( strKey + "auto_check_mode", "r").toString();

no problem with the code compiling & running. when i try to print strMode program is unexpectedly finising.. this is happening with QString, Date (Derived data types). but fine with basic data types like int, float, bool...
using printf to print the values. qDebug is not working in this project. what could be the problem?

Santosh Reddy
16th June 2011, 08:14
Does your derived data type(Date) support QVariant ?
QSettings use QVariant to work.