PDA

View Full Version : QSettings - reading from ini files with sections/groups



el
18th November 2009, 10:00
Hello,

i am new to Qt and trying to build a small application right now which should save its configuration to an .ini file which is checked again on app-launch to do the initial gui filling of my app.

Basically my ini has the following structure


[item01]
id=foo
path=bar

[item02]
id=bla
path=blub

Right now i am wondering what might be the best way to read the entire file at the beginning of my application.

My idea is to count the amount of groups / sections in the ini first.
In the next step i could fill my UI (QListWidget) element with an QListWidget item for each group of my ini file.

While i know that i.e.

//
QSettings settings( "myininame.ini", QSettings::IniFormat );
// keys contains all elements of settings
QStringList keys = settings.allKeys();

works and delivers me all relevant data - its not offering any sorting-option i would like.
Reading and writing single elements from/to the .ini works too.
So i am unsure how to realize the loop-structure to handle each section/group on itself.
It might help to i.e. count all sections/group at the beginning - but still not sure if i am thinking wrong here.


I hope my main starting problem is clear- would be great if someone could push my into the right direction - right now i just dont know the amount of sections/groups which makes it difficult for me to know when to end the loop ;)


Best regards
el

miwarre
18th November 2009, 21:50
Have you tried QSettings::childKeys() ?

If used without using any beginGroup(), it returns a string list with all the top level keys ( ["item01", "item02"] in your example). From the list, you can iterate and get the sub-keys of each top-level key.

childKeys() returns all top-level keys, while childGroups() only returns top-level keys which actually have sub-keys.

For your example (and in general for INI-like files) this makes no difference, but for registry-based settings, it might.

M.

el
20th November 2009, 08:57
Hi miwarre

first of all thanks for the reply abnd pointing me into the direction.

Right now i am wondering why my first attempt does not output anything - but i guess i am still having small issues in my code.


ini:


[0]
id=foo
path=bar
[1]
id=bar
path=bla


Code:


QSettings settings ("myinifile.ini", QSettings::IniFormat);

qDebug() << "test";
QStringList childKeys = settings.childKeys();
foreach (const QString &childKey, childKeys)
qDebug() << settings.value(childKey);


and



QSettings settings ("myinifile.ini", QSettings::IniFormat);
QStringList keys = settings.childKeys();
qDebug() << keys;


seems to end in an empty list too.



but all in all i guess childKeys was a good hint - so thanks again
Best regards
el

el
20th November 2009, 14:32
solved it - thanks again for the helping hand