Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Put QList items inside a QMap
Qt 6.12 LTS is out! See what's new in the release blog

Put QList items inside a QMap

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 4.5k 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.
  • M Mr Gisa

    I was wondering if it's possible to add a QList<QString> values inside of a QMap<QString, int> without the need to loop over QList. Is there a Qt or C++ std API for that? All the ints will be false.

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

    @Mr-Gisa
    (I'm sure you can save a tiny bit of code via a std:: method.) But does it really matter? Isn't the time taken to loop through the list trivial, at least compared to creating the items in the map, which ultimately I presume must be done one at a time?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mr Gisa
      wrote on last edited by Mr Gisa
      #3

      Actually it doesn't matter, I'm just curious, I was wondering and decided to ask.

          for (const auto &item : qAsConst(items)) {
              mItems.insert(item, false);
          }
      

      That is how I'm doing it.

      JonBJ Gojir4G 2 Replies Last reply
      0
      • M Mr Gisa

        Actually it doesn't matter, I'm just curious, I was wondering and decided to ask.

            for (const auto &item : qAsConst(items)) {
                mItems.insert(item, false);
            }
        

        That is how I'm doing it.

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

        @Mr-Gisa
        That seems fine! Ultimately there's not going to be a "create many map keys from many input keys all in one go" low-level functionality, so you will still have to iterate through the inputs.

        What you could do is if your input list is sorted by key already, look at iterator QMap::insert(const_iterator pos, const Key &key, const T &value) to save insertion look-up time. However, if your input list is not already in sorted order, it won't be worth sorting it first! :)

        There's also QMap::QMap(std::initializer_list<std::pair<Key, T> > list), but I don't think you start with a suitable initializer list format....

        1 Reply Last reply
        1
        • M Mr Gisa

          Actually it doesn't matter, I'm just curious, I was wondering and decided to ask.

              for (const auto &item : qAsConst(items)) {
                  mItems.insert(item, false);
              }
          

          That is how I'm doing it.

          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by
          #5

          @Mr-Gisa You may consider using only one QMap, except if you need to keep insertion order of your QList. Then you can access the list when needed using QMap::keys().

          1 Reply Last reply
          0
          • M Mr Gisa

            I was wondering if it's possible to add a QList<QString> values inside of a QMap<QString, int> without the need to loop over QList. Is there a Qt or C++ std API for that? All the ints will be false.

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

            @Mr-Gisa said in Put QList items inside a QMap:

            All the ints will be false.

            BTW, if this is always the case, you don't need a QMap and a QHash will be quicker/smaller.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mr Gisa
              wrote on last edited by
              #7

              @Gojir4 said in Put QList items inside a QMap:

              QMap

              What you mean with using only one QMap? I'm using only one QMap already.

              @JonB

              BTW, if this is always the case, you don't need a QMap and a QHash will be quicker/smaller.

              Yes, it's a QMap<QString, bool>. In this case is better to use a QHash? Why?

              Gojir4G 2 Replies Last reply
              0
              • M Mr Gisa

                @Gojir4 said in Put QList items inside a QMap:

                QMap

                What you mean with using only one QMap? I'm using only one QMap already.

                @JonB

                BTW, if this is always the case, you don't need a QMap and a QHash will be quicker/smaller.

                Yes, it's a QMap<QString, bool>. In this case is better to use a QHash? Why?

                Gojir4G Offline
                Gojir4G Offline
                Gojir4
                wrote on last edited by
                #8

                @Mr-Gisa What I mean is to replace the QList with QMap (or QHash)

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mr Gisa
                  wrote on last edited by
                  #9

                  The QList is an argument of a method, I don't have two containers in the class.

                  1 Reply Last reply
                  0
                  • M Mr Gisa

                    @Gojir4 said in Put QList items inside a QMap:

                    QMap

                    What you mean with using only one QMap? I'm using only one QMap already.

                    @JonB

                    BTW, if this is always the case, you don't need a QMap and a QHash will be quicker/smaller.

                    Yes, it's a QMap<QString, bool>. In this case is better to use a QHash? Why?

                    Gojir4G Offline
                    Gojir4G Offline
                    Gojir4
                    wrote on last edited by
                    #10

                    @Mr-Gisa said in Put QList items inside a QMap:

                    Yes, it's a QMap<QString, bool>. In this case is better to use a QHash? Why?

                    From the doc of QMap:

                    QMap and QHash provide very similar functionality. The differences are:

                    • QHash provides average faster lookups than QMap. (See Algorithmic Complexity for details.)

                    • When iterating over a QHash, the items are arbitrarily ordered. With QMap, the items are always sorted by key.

                    • The key type of a QHash must provide operator==() and a global qHash(Key) function. The key type of a QMap must provide operator<() specifying a total order. Since Qt 5.8.1 it is also safe to use a pointer type as key, even if the underlying operator<() does not provide a total order.

                    1 Reply Last reply
                    2
                    • M Offline
                      M Offline
                      Mr Gisa
                      wrote on last edited by
                      #11

                      Thank you guys, helped a lot.

                      JonBJ 1 Reply Last reply
                      0
                      • M Mr Gisa

                        Thank you guys, helped a lot.

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

                        @Mr-Gisa
                        I was actually going to say:

                        If you have nothing useful to store for the "value" part of a QHash/QMap, your case could actually suffice with a QSet. The advantage being (given that QSet is only really hiding a QHash anyway), there is a QSet<T> QSet::fromList(const QList<T> &list) (http://doc.qt.io/qt-5/qset.html#fromList) static method which would satisfy for sure your adding of all the strings in your list without you having to write any iteration code/function :)

                        1 Reply Last reply
                        3

                        • Login

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