[SOLVED]how to traverse QMap as this QMap< QString, QMap< QString,QStringList > >?



  • Hi,all.I'm faced a problem that traverse an QMap like this QMap< QString, QMap< QString,QStringList > >,but I failed.Can someone give me an advice?



  • Hi.
    and welcome to Qt devnet.

    You can do it in either way.
    Qt provides two ways : Java-style iterator and STL-style iterator

    1 .Java-style iterator

    @
    QMap< QString, QMap< QString,QStringList > > map1;
    QMap< QString,QStringList > map2,temp;

    QMapIterator<QString, QMap< QString,QStringList > > i1(map1);
    while (i1.hasNext())
    {
    i1.next();
    qDebug() <<"@"<< i1.key() << ": " << i1.value() << endl;

    temp = i1.value();

    QMapIterator<QString, QStringList> i2(temp);
    while (i2.hasNext())
    {
    i2.next();
    qDebug()<<"@@" <<i2.key() << ": " << i2.value() << endl;
    QStringList strListTemp=i2.value();
    foreach(QString str,strListTemp)
    {
    qDebug()<<"list :"<<str;
    }
    }

    }

    @

    2.STL-style iterator

    @
    QMap< QString, QMap< QString,QStringList > >::const_iterator i1 = map1.constBegin();
    while (i1 != map1.constEnd())
    {
    qDebug() << i1.key() << ": " << i1.value() << endl;
    temp = i1.value();

    QMap< QString,QStringList > ::const_iterator i2 = temp.constBegin();

    while (i2 != temp.constEnd())
    {
    qDebug() << i2.key() << ": " << i2.value() << endl;
    QStringList strListTemp=i2.value();
    foreach(QString str,strListTemp)
    {
    qDebug()<<"list :"<<str;
    }
    ++i2;

    }
    ++i1;

    }
    @

    Hope this helps.



  • Thanks a lots! And how can i insert an item to the QStringList in the map?



  • Hi.
    it is not a tough ask.
    you should do it your owns.
    The logic should be yours.i just can guide you ;not to do your job.

    BTW. There is a lot of insertion functions/mechanism.Have a look on Qt Docs and examples.



  • Thanks again for reply.i'm a newbiee and faced this problem for long time. with your help,i resolved this problem finally.before that,i used your code with error.



  • That's nice.
    Now Please add thread title as [SOLVED] so other members can notify that soultion has been found.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.