Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with QMap
Forum Updated to NodeBB v4.3 + New Features

Problem with QMap

Scheduled Pinned Locked Moved Solved General and Desktop
qmap
15 Posts 3 Posters 5.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cristiano.narcisi
    wrote on 25 May 2016, 12:38 last edited by
    #1

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

    #ifndef CST2220CONFIGPARAMETERMAP_H
    #define CST2220CONFIGPARAMETERMAP_H
    
    #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>
    {
    public:
        Cst2220ConfigParameterMap(  QString mac,
                                    QString ip,
                                    QString cst2220_apk_path,
                                    QString bws_apk_path,
                                    QString videx_folder_path);
    };
    
    #endif // CST2220CONFIGPARAMETERMAP_H
    
    

    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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 25 May 2016, 12:46 last edited by
      #2

      @cristiano.narcisi said:

      QMap

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

      C K 2 Replies Last reply 25 May 2016, 13:07
      0
      • M mrjj
        25 May 2016, 12:46

        @cristiano.narcisi said:

        QMap

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

        C Offline
        C Offline
        cristiano.narcisi
        wrote on 25 May 2016, 13:07 last edited by
        #3

        thanks @mrjj

        here the constructor

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

        this->insert(MAC_KEY,mac);
        this->insert(IP_KEY,ip);
        this->insert(CST2220_APK_KEY,cst2220_apk_path);
        this->insert(BWS_APK_KEY,bws_apk_path);
        this->insert(VIDEX_FOLDER_KEY,videx_folder_path);
        

        }

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 25 May 2016, 13:11 last edited by
          #4

          Ok, it does look perfect valid.

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

          C 1 Reply Last reply 25 May 2016, 13:13
          1
          • M mrjj
            25 May 2016, 13:11

            Ok, it does look perfect valid.

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

            C Offline
            C Offline
            cristiano.narcisi
            wrote on 25 May 2016, 13:13 last edited by
            #5

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

            M 1 Reply Last reply 25 May 2016, 13:14
            0
            • C cristiano.narcisi
              25 May 2016, 13:13

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

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 25 May 2016, 13:14 last edited by
              #6

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

              C 1 Reply Last reply 25 May 2016, 13:22
              1
              • M mrjj
                25 May 2016, 13:14

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

                C Offline
                C Offline
                cristiano.narcisi
                wrote on 25 May 2016, 13:22 last edited by
                #7

                @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!!!!

                M 1 Reply Last reply 25 May 2016, 13:30
                1
                • C cristiano.narcisi
                  25 May 2016, 13:22

                  @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!!!!

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 25 May 2016, 13:30 last edited by
                  #8

                  @cristiano.narcisi
                  OK ?!?! That also pretty strange.

                  what about size inside and out?

                  C 1 Reply Last reply 25 May 2016, 13:38
                  0
                  • M mrjj
                    25 May 2016, 13:30

                    @cristiano.narcisi
                    OK ?!?! That also pretty strange.

                    what about size inside and out?

                    C Offline
                    C Offline
                    cristiano.narcisi
                    wrote on 25 May 2016, 13:38 last edited by
                    #9

                    @mrjj

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

                    M 1 Reply Last reply 25 May 2016, 13:46
                    1
                    • C Offline
                      C Offline
                      cristiano.narcisi
                      wrote on 25 May 2016, 13:45 last edited by
                      #10

                      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();```
                      
                      1 Reply Last reply
                      0
                      • C cristiano.narcisi
                        25 May 2016, 13:38

                        @mrjj

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

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 25 May 2016, 13:46 last edited by
                        #11

                        @cristiano.narcisi
                        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.

                        C 1 Reply Last reply 25 May 2016, 14:18
                        0
                        • M mrjj
                          25 May 2016, 13:46

                          @cristiano.narcisi
                          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.

                          C Offline
                          C Offline
                          cristiano.narcisi
                          wrote on 25 May 2016, 14:18 last edited by
                          #12

                          @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

                          Regards

                          Cristiano Narcisi

                          M 1 Reply Last reply 25 May 2016, 18:31
                          2
                          • M mrjj
                            25 May 2016, 12:46

                            @cristiano.narcisi said:

                            QMap

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

                            K Offline
                            K Offline
                            kshegunov
                            Moderators
                            wrote on 25 May 2016, 17:22 last edited by
                            #13

                            @mrjj

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

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            1
                            • C cristiano.narcisi
                              25 May 2016, 14:18

                              @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

                              Regards

                              Cristiano Narcisi

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 25 May 2016, 18:31 last edited by
                              #14

                              @cristiano.narcisi
                              ok so
                              #define MAC_KEY "mac"
                              used as key did funky stuff?

                              good found :)

                              K 1 Reply Last reply 25 May 2016, 18:43
                              0
                              • M mrjj
                                25 May 2016, 18:31

                                @cristiano.narcisi
                                ok so
                                #define MAC_KEY "mac"
                                used as key did funky stuff?

                                good found :)

                                K Offline
                                K Offline
                                kshegunov
                                Moderators
                                wrote on 25 May 2016, 18:43 last edited by
                                #15

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

                                Read and abide by the Qt Code of Conduct

                                1 Reply Last reply
                                1

                                7/15

                                25 May 2016, 13:22

                                • Login

                                • Login or register to search.
                                7 out of 15
                                • First post
                                  7/15
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved