1

I have a XML-file holding among other things some groups with name and userlists. In my code in constructor I have set a dictionary for this list:

dictGroups= QMap<QString, QList<QString>>() ;

In headerfile it is declared as

public:
QMap<QString, QList<QString>> dictGroups;

Then I read the file: ReadConfig();

void AppConfig::ReadConfig(void)
{
    ...
while(!reader.atEnd())
{
    ReadGroups(reader);
    if (dictGroups.isEmpty()) qDebug()<<"ReadConfig_isEmpty";
}
    ...

This is my ReadGroups:

void AppConfig::ReadGroups(QXmlStreamReader &reader)
{
    dictGroups.clear();
    while(!reader.atEnd())
    {
        reader.readNext();
        if (reader.error())
        {
...
        }
        else
        {
            if (reader.isStartElement())
            {
                if (reader.name().toString().toLower()=="group"){
                    ReadGroup(reader);
                    if (dictGroups.isEmpty()) qDebug()<<"ReadGroups_isEmpty";
                }
            }
            else if (reader.isEndElement())
            {
                if (reader.name().toString().toLower() == "groups")
                {
                    if(dictGroups.count()<=0){
                        QList<QString> users= QList<QString>();
                        users.append(this->GetUsername());
                        dictGroups.insert("admin", users);
                    }
                    return;
                }
            }
        }
    }
}

My problem is, that the items inserted in dictGroups while ReadGroups get lost. I get the debug output

ReadConfig_isEmpty

but in ReadGroups seems everything is ok. I'm at a loss, puzzling around for hours, can anybody help to find the reason?

Frau Schmidt
  • 152
  • 11
  • Are you absolutely positive that you insert anything into `dictGroups`? – thuga Jun 13 '16 at 12:49
  • Yes :-( My debug prints me insert messages and ReadGroups_isEmpty will not be written. Also it doesn't insert the 'admin' -user, if dict would be empty, because at this step it isn't empty. – Frau Schmidt Jun 13 '16 at 12:56
  • 1
    Difficult to say anything without an [SSCCE](http://sscce.org). – thuga Jun 13 '16 at 13:02
  • 1
    just a comment: the line in the constructor does nothing useful and can be removed (the `dictGroups= QMap>() ;`) – Karsten Koop Jun 13 '16 at 13:56
  • 2
    I thought the purpose of a map was to *find* things, not lose them! – rubenvb Jun 13 '16 at 14:57
  • @KarstenKoop removed the line and it works anyway. There I have a problem in understanding. I only declare it and not have to initialize it ? – Frau Schmidt Jun 14 '16 at 07:07
  • The default constructor of the member variable is called automatically. What you were doing is create a new instance of `QMap` and assign it to your instance, which copies the contents ([see `operator =`](http://doc.qt.io/qt-5/qmap.html#operator-eq)), but both maps are empty. – Karsten Koop Jun 14 '16 at 07:09
  • @rubenvb If I ever get enough reputation to upvote, you'll get my first for being so lucky ;-) – Frau Schmidt Jun 14 '16 at 07:13
  • @KarstenKoop Is it because its a value type? Reference Types I have to initialize with operater =. The difference between value and reference types I've learned few days ago having a similar problem with QList. So I was insecure. So maybe the question is really off-topic. Nevertheless for me the correspondence was very helpful. Thanks! – Frau Schmidt Jun 14 '16 at 07:25
  • I guess you are mixing up Java/C# with C++ – Karsten Koop Jun 14 '16 at 07:26
  • @KarstenKoop Yes, C# is my 'home' , and if I have to do sth. with c++ coming to my personally pointer hell ;-) won't to querrel, I'll get on with it. – Frau Schmidt Jun 14 '16 at 07:32
  • @rubenvb +1 I got it :-) – Frau Schmidt Jun 14 '16 at 10:40

1 Answers1

1

You have this code:

dictGroups.clear();

Why do you expect the dictGroups to persist when you clear them on every iteration of the outer loop? Don't do that.

The clear statement belongs perhaps at the beginning of ReadConfig.

Your method name capitalizations are very much out of place in Qt code, though: capitalized names are by convention reserved for groups.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • You're right, the method ReadGroups is called a second time when coming through the xml-end-tag, a stupid mistake, thank you a lot. – Frau Schmidt Jun 14 '16 at 07:01
  • ever thought, formatting my code is "matter of taste", now I looked for a regulation but found this [link](http://stackoverflow.com/q/1776291/2747431) – Frau Schmidt Jun 14 '16 at 07:04