Problem with QMap

  • Hi all. I am using the class in the snippet below to pass several parameters to a function

    #include <QMap>
    #include <QString>
    #include <QDebug>
    #define MAC_KEY             "mac"
    #define IP_KEY              "ip"
    #define CST2220_APK_KEY     "cst2220apk"
    #define BWS_APK_KEY         "bwsapk"
    #define VIDEX_FOLDER_KEY    "videx_folder"
    class Cst2220ConfigParameterMap : public QMap<const char *,QString>
        Cst2220ConfigParameterMap(  QString mac,
                                    QString ip,
                                    QString cst2220_apk_path,
                                    QString bws_apk_path,
                                    QString videx_folder_path);

    I have a strange problem. I can retry the key-value pair only within the constructor and not from outside.

    if( this->contains(MAC_KEY))...

    return true.

         Cst2220ConfigParameterMap par(mMacAddrMan->getMac(),mIpAddrMan->getIp(),pathCST2220Apk,pathBWSApk,pathVidexFolderApk);
         if( par.contains(MAC_KEY))

    returns false!!!!

    What am i doing wrong?

    I'm getting crazy!

    thank you for helping me

    Cristiano Narcisi

  • Lifetime Qt Champion

    @cristiano.narcisi said:


    can you show the cpp of the constructor also?
    How you add the items.

    Inheriting from Qmap is a bit funky for me as with STL that was normally
    not super idea. but i dont know with QList. :)

  • thanks @mrjj

    here the constructor

    Cst2220ConfigParameterMap::Cst2220ConfigParameterMap( QString mac,
    QString ip,
    QString cst2220_apk_path,
    QString bws_apk_path,
    QString videx_folder_path) : QMap(){



  • Lifetime Qt Champion

    Ok, it does look perfect valid.

    have you tried
    qDebug() << size(); (in ctor)
    qDebug() << par.size(); (right after u define par)

  • Ok. But what does it happen when the function is contains(..key..)?

  • Lifetime Qt Champion

    it looks like outside of constructor the key list is no more.
    i wanted to see if it says
    zero for par.

  • @mrjj

    Ok but it is very strange. I give you another information: i can view all the key-value pair stored into the par variable using the watches of the editor!!!!

  • Lifetime Qt Champion

    OK ?!?! That also pretty strange.

    what about size inside and out?

  • @mrjj

    Size outside the ctor is correct. It is 5!!!

  • Using the iteartor i can view all the values stored in the QMap .... Probably i don't understand something of very important about how QMap acceses the data.
    //your code here

     QMap<const char *, QString>::ConstIterator ii;
     for( ii = par.constBegin(); ii != par.constEnd(); ++ii )
       qDebug() << ii.key() << " = " << ii.value();```

  • Lifetime Qt Champion

    ok ?!?

    I think you should try dump the list using par to see if key corruption
    else there is no reason contains should not work.

  • @mrjj

    Probably i have understood why i can retry the key. The problem is that i am using

    QMap< const char *, QString> 

    This means that the key is a POINTER and not the text !!!

    I have replaced the QMap template with

    QMap< QString, QString>

    Now the key is an ITEM and not a POINTER and i can find all the keys and the value stored within the QMap.

    Sorry for bothering you

    Thanks for hepling me


    Cristiano Narcisi

  • Qt Champions 2017


    Inheriting from Qmap is a bit funky

    It's specially noted in the docs that deriving from a container specialization is not recommended. That's probably what seems funky ;)

  • Lifetime Qt Champion

    ok so
    #define MAC_KEY "mac"
    used as key did funky stuff?

    good found :)

  • Qt Champions 2017

    @mrjj said:

    used as key did funky stuff?

    The only funky stuff with that macro is that char * doesn't have a meaningful operator <, which is required for it to be a key in the map. The comparison is done as you'd compare integers.

Log in to reply

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