[Interest] Reading trees from QSettings

Sensei senseiwa at gmail.com
Thu Aug 30 19:27:44 CEST 2012

On 8/30/12 4:39pm, BRM wrote:
> I'm saying to do something like:
> [Root]
> item\1\name=New File
> item\1\isFile=true
> item\2\name=New Folder
> item\2\isFile=false
> item\size=2
> [New%20Folder]
> item\1\name=aaaa
> item\1\isFile=false
> item\size=1
> No QStringList is required, and it's very straight forward. Your
> QSettings values expand, but in a meaningful way.

I understand. I'd like to avoid extra parameters, if not necessary.

Your solution works flawlessly, thanks!

>> and I've found that the problem is in calling QSettings
>> with QStringList, as you can see in my reply to my previous post.
>>       QStringList sons = s.childGroups();
>>       // in the loop:
>>       qWarning("evaluating '%s', child %d %d",
>>          qPrintable(name),
>>          s.childGroups().contains(name) == true,
>>          sons.contains(name) == true);
>> The two comparisons should yield true or false, but I get different results.
>> That is weird, to say the least...
> Are you sure 'sons' and 's.childGroups()' are the same thing? If you
> don't update 'sons' in the loop, then they're not likely the same
> thing. IIRC, 's' is manipulated as you move through the file - thus
> when you do a s.beginGroup()/s.endGroup(), then s.childGroups() will
> not be the same as that of the "root" (the file in general); it will
> only tell you what is below the level you are looking at. You can get
> around that by not using s.beginGroup()/s.endGroup() and instead
> using "group/group" and "group/value" syntax when looking something
> up, but that would carry its own drawbacks (such as dynamically
> computing the "group/group" and "group/value" strings yourself
> instead of letting QSettings handle them for you).

Ok, that makes sense. But I am wondering: how could childGroups() 
differ, in an INI file, since it does not support nested groups?

In fact, I still wonder that! You are totally right: if print the 
childgroups, I get an empty string *inside* the loop.

This is an unexpected behavior, I am using INI, knowing that it is a 
flat file, so I was expecting that childGroups() would always return the 
global scope. I was wrong.

> I still suggest adding the extra parameter as I noted above. It fits
> with the array method vary easily - you just pull out the two values
> in the loop (from within the beginGroup()/endGroup() while processing
> the array) and you know what to do - add the file, or add a folder
> and look up another group:
> QStringList sons = s.childGroups();
> // sons == s.childGroups()
> s.beginGroup("root");
>      // sons != s.childGroups()
>      arraySize = s.beginReadArray("items");
>      for (...)
>          {
>          bool isGroup = s.value("isFile", false).toBool();
>          QString name = s.value("name").toString();
>          if (isGroup) addFolder(name);
>          else addFile(name);
>          }
> s.endGroup();
> // sons == s.childGroups()

Do you think that caching the global scope could bring other problems? I 
like zen files...

> $0.02

Your help is much more valuable that two cents ;)

More information about the Interest mailing list