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. How To print all member QMap with correct key
Forum Updated to NodeBB v4.3 + New Features

How To print all member QMap with correct key

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 2.6k Views 1 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.
  • R Offline
    R Offline
    rifky
    wrote on last edited by
    #1

    i need the correct key , how to fix my code , i'm using devnum_bbu_slotid.key(slot_id) for getting key, here's my snipet .

    thanks.

    data:

    QMap(("L1071905001", 6)("L1071905001", 3)("L1081905001", 7)("L1081905001", 6))
    

    code:

    QMap<QString,int> devnum_bbu_slotid;
    foreach (int slot_id , devnum_bbu_slotid){
                qDebug()<<"-- key:"<<devnum_bbu_slotid.key(slot_id)<<" value:"<<slot_id;
    }
    

    output :

    -- key: "L1071905001"  value: 6
    -- key: "L1071905001"  value: 3
    -- key: "L1081905001"  value: 7
    -- key: "L1071905001"  value: 6
    
    JonBJ 1 Reply Last reply
    0
    • R rifky

      i need the correct key , how to fix my code , i'm using devnum_bbu_slotid.key(slot_id) for getting key, here's my snipet .

      thanks.

      data:

      QMap(("L1071905001", 6)("L1071905001", 3)("L1081905001", 7)("L1081905001", 6))
      

      code:

      QMap<QString,int> devnum_bbu_slotid;
      foreach (int slot_id , devnum_bbu_slotid){
                  qDebug()<<"-- key:"<<devnum_bbu_slotid.key(slot_id)<<" value:"<<slot_id;
      }
      

      output :

      -- key: "L1071905001"  value: 6
      -- key: "L1071905001"  value: 3
      -- key: "L1081905001"  value: 7
      -- key: "L1071905001"  value: 6
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @rifky said in How To print all member QMap with correct key:

      i need the correct key , how to fix my code

      ? What do you mean? You add duplicate keys into the QMap, and that is what your code prints out. If you mean you want to know the key for, say, value of 7 then obviously just print out devnum_bbu_slotid.key(7)?

      R 1 Reply Last reply
      0
      • JonBJ JonB

        @rifky said in How To print all member QMap with correct key:

        i need the correct key , how to fix my code

        ? What do you mean? You add duplicate keys into the QMap, and that is what your code prints out. If you mean you want to know the key for, say, value of 7 then obviously just print out devnum_bbu_slotid.key(7)?

        R Offline
        R Offline
        rifky
        wrote on last edited by
        #3

        @JonB said in How To print all member QMap with correct key:

        @rifky said in How To print all member QMap with correct key:

        i need the correct key , how to fix my code

        ? What do you mean? You add duplicate keys into the QMap, and that is what your code prints out. If you mean you want to know the key for, say, value of 7 then obviously just print out devnum_bbu_slotid.key(7)?

        i mean,
        L1071905001 have 2 values and L1081905001 have 2 values ,
        i want data printed like this,

        -- key: "L1071905001"  value: 6
        -- key: "L1071905001"  value: 3
        -- key: "L1081905001"  value: 7
        -- key: "L1081905001"  value: 6
        

        but this code print L1081905001 just have 1 value, because one of the values same(6) as L1071905001

        JonBJ 2 Replies Last reply
        0
        • R rifky

          @JonB said in How To print all member QMap with correct key:

          @rifky said in How To print all member QMap with correct key:

          i need the correct key , how to fix my code

          ? What do you mean? You add duplicate keys into the QMap, and that is what your code prints out. If you mean you want to know the key for, say, value of 7 then obviously just print out devnum_bbu_slotid.key(7)?

          i mean,
          L1071905001 have 2 values and L1081905001 have 2 values ,
          i want data printed like this,

          -- key: "L1071905001"  value: 6
          -- key: "L1071905001"  value: 3
          -- key: "L1081905001"  value: 7
          -- key: "L1081905001"  value: 6
          

          but this code print L1081905001 just have 1 value, because one of the values same(6) as L1071905001

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @rifky
          Ah, I see, with the keys you picked being so similar I didn't even notice that there are two different keys! You might have drawn the reader's attention to this!

          So instead of using key() ("Returns the first key with value value") you need to call QList<Key> QMap::keys(const T &value) const, which

          Returns a list containing all the keys associated with value value in ascending order.

          This function can be slow (linear time), because QMap's internal data structure is optimized for fast lookup by key, not by value.

          So you get a list of all the keys with that value, and do what you will with those.

          Or, don't try to lookup by value (you'll need to know all the values), just iterate the whole QMap and you will meet the multiple keys which (happen to) have the same value. There is no "correct key" for a value of 6, there are simply multiple keys with that value.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Bonnie
            wrote on last edited by
            #5

            Hey, I assume that you are using insertMulti() to add values since you have different values with the same key.
            That makes you QMap actually a QMultiMap.
            So referring to the foreach doc:

            QMultiMap<QString, int> map;
            ...
            foreach (const QString &str, map.uniqueKeys()) {
            foreach (int i, map.values(str))
            qDebug() << str << ':' << i;
            }

            Your code should be

            foreach (const QString &key, devnum_bbu_slotid.uniqueKeys()) {
                foreach (int slot_id, devnum_bbu_slotid.values(key))
                    qDebug()<<"-- key:"<<key<<" value:"<<slot_id;
            }
            
            1 Reply Last reply
            3
            • R rifky

              @JonB said in How To print all member QMap with correct key:

              @rifky said in How To print all member QMap with correct key:

              i need the correct key , how to fix my code

              ? What do you mean? You add duplicate keys into the QMap, and that is what your code prints out. If you mean you want to know the key for, say, value of 7 then obviously just print out devnum_bbu_slotid.key(7)?

              i mean,
              L1071905001 have 2 values and L1081905001 have 2 values ,
              i want data printed like this,

              -- key: "L1071905001"  value: 6
              -- key: "L1071905001"  value: 3
              -- key: "L1081905001"  value: 7
              -- key: "L1081905001"  value: 6
              

              but this code print L1081905001 just have 1 value, because one of the values same(6) as L1071905001

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @rifky
              As per @Bonnie, if you are not using a QMultiMap (you don't seem to use that) you do not have any multiple values against a given key in a QMap: the last one added replaces the previous one. If you do want multi-values per key, you must change over to QMultiMap, and then you can use @Bonnie's code.

              B 1 Reply Last reply
              1
              • JonBJ JonB

                @rifky
                As per @Bonnie, if you are not using a QMultiMap (you don't seem to use that) you do not have any multiple values against a given key in a QMap: the last one added replaces the previous one. If you do want multi-values per key, you must change over to QMultiMap, and then you can use @Bonnie's code.

                B Offline
                B Offline
                Bonnie
                wrote on last edited by Bonnie
                #7

                @JonB said in How To print all member QMap with correct key:

                if you are not using a QMultiMap (you don't seem to use that) you do not have any multiple values against a given key in a QMap

                Actually he can...
                QMap::insertMulti is obsolete, but still exists...

                JonBJ 1 Reply Last reply
                1
                • R Offline
                  R Offline
                  rifky
                  wrote on last edited by
                  #8

                  Sorry for late respon, i try to change this code , i've found another solution, before i'm using devnum_bbu_slotid.insertMulti

                  QMultiMap<QString,int> devnum_bbu_slotid;
                  QMapIterator<QString, int> x(devnum_bbu_slotid);
                   while (x.hasNext()) {
                       x.next();
                       qDebug()<<"-- key:"<<x.key()<< "value:" <<x.value();
                   }
                  

                  it's worked

                  B 1 Reply Last reply
                  0
                  • B Bonnie

                    @JonB said in How To print all member QMap with correct key:

                    if you are not using a QMultiMap (you don't seem to use that) you do not have any multiple values against a given key in a QMap

                    Actually he can...
                    QMap::insertMulti is obsolete, but still exists...

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @Bonnie
                    The help page for the latest (https://doc.qt.io/qt-5/qmap.html) has no QMap::insertMulti, and states

                    Normally, a QMap allows only one value per key. If you call insert() with a key that already exists in the QMap, the previous value will be erased. For example:
                    However, you can store multiple values per key by using using the subclass QMultiMap. If you want to retrieve all the values for a single key, you can use values(const Key &key), which returns a QList<T>:

                    So we wouldn't want to encourage the OP to use any undocumented/obsolete API, would we? ;-)

                    B 1 Reply Last reply
                    0
                    • R rifky

                      Sorry for late respon, i try to change this code , i've found another solution, before i'm using devnum_bbu_slotid.insertMulti

                      QMultiMap<QString,int> devnum_bbu_slotid;
                      QMapIterator<QString, int> x(devnum_bbu_slotid);
                       while (x.hasNext()) {
                           x.next();
                           qDebug()<<"-- key:"<<x.key()<< "value:" <<x.value();
                       }
                      

                      it's worked

                      B Offline
                      B Offline
                      Bonnie
                      wrote on last edited by
                      #10

                      @rifky
                      Right! Iterator is the first thing comes to my mind.
                      But I think maybe you prefer foreach :)

                      1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Bonnie
                        The help page for the latest (https://doc.qt.io/qt-5/qmap.html) has no QMap::insertMulti, and states

                        Normally, a QMap allows only one value per key. If you call insert() with a key that already exists in the QMap, the previous value will be erased. For example:
                        However, you can store multiple values per key by using using the subclass QMultiMap. If you want to retrieve all the values for a single key, you can use values(const Key &key), which returns a QList<T>:

                        So we wouldn't want to encourage the OP to use any undocumented/obsolete API, would we? ;-)

                        B Offline
                        B Offline
                        Bonnie
                        wrote on last edited by
                        #11

                        @JonB said in How To print all member QMap with correct key:

                        The help page for the latest (https://doc.qt.io/qt-5/qmap.html) has no QMap::insertMulti

                        It is in the Obsolete members page.

                        So we wouldn't want to encourage the OP to use any undocumented/obsolete API, would we? ;-)

                        Of course!
                        I was just assuming the OP is already using it so he may think what you said is not true...

                        1 Reply Last reply
                        1
                        • R Offline
                          R Offline
                          rifky
                          wrote on last edited by
                          #12

                          @JonB said in How To print all member QMap with correct key:

                          @Bonnie
                          The help page for the latest (https://doc.qt.io/qt-5/qmap.html) has no QMap::insertMulti, and states

                          Normally, a QMap allows only one value per key. If you call insert() with a key that already exists in the QMap, the previous value will be erased. For example:
                          However, you can store multiple values per key by using using the subclass QMultiMap. If you want to retrieve all the values for a single key, you can use values(const Key &key), which returns a QList<T>:

                          So we wouldn't want to encourage the OP to use any undocumented/obsolete API, would we? ;-)

                          i'm using QC 4.11, its documented well

                          QMap::iterator QMap::insertMulti(const Key &key, const T &value)
                          Inserts a new item with the key key and a value of value.
                          If there is already an item with the same key in the map, this function will simply create a new one. (This behavior is different from insert(), which overwrites the value of an existing item.)
                          See also insert() and values().

                          @Bonnie said in How To print all member QMap with correct key:

                          @rifky
                          Right! Iterator is the first thing comes to my mind.
                          But I think maybe you prefer foreach :)

                          yeah ,, i got 2 solution at the same time , i'll use your code ,, that's more readable for me :)

                          solved, thanks @JonB and @Bonnie for the quick response and helping me

                          1 Reply Last reply
                          0

                          • Login

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