[Interest] Reading trees from QSettings

BRM bm_witness at yahoo.com
Thu Aug 30 16:39:55 CEST 2012


> From: Sensei <senseiwa at gmail.com>

> Subject: Re: [Interest] Reading trees from QSettings
> 
> On 8/30/12 3:37pm, BRM wrote:
>>  While I don't know if there is a "safe way to [query] for an 
> existing
>>  group", I'd suggest that you just give yourself another key as 
> part
>>  of the array to identify it as either a file or group.
> 
> I've tried that

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.

> 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).

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()

$0.02

Ben




More information about the Interest mailing list