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. QMap, index of entry without iterating through
Forum Updated to NodeBB v4.3 + New Features

QMap, index of entry without iterating through

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 5 Posters 1.8k 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.
  • J.HilkJ J.Hilk

    @SPlatten If I understand you correctly, then keys() is still the function to call.

    it should be ordered by the order of insertion into the QMap, except duplicated keys of course

    SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #5

    @J-Hilk, do I then iterator through keys? I was hoping for something where I could supply a key to look up and it returns the index of the entry? As I said this is easy enough to write, just didn't want to do it if its already available.

    The other part of this problem:
    https://forum.qt.io/topic/131333/ui-finding-control-by-using-literal-string

    Once I have the key I want to use it to construct a control name, but how do I find the control using a string?

    Kind Regards,
    Sy

    J.HilkJ KroMignonK 2 Replies Last reply
    0
    • SPlattenS SPlatten

      @J-Hilk, do I then iterator through keys? I was hoping for something where I could supply a key to look up and it returns the index of the entry? As I said this is easy enough to write, just didn't want to do it if its already available.

      The other part of this problem:
      https://forum.qt.io/topic/131333/ui-finding-control-by-using-literal-string

      Once I have the key I want to use it to construct a control name, but how do I find the control using a string?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #6

      @SPlatten

      myMap.keys().indexOf("someKey") ?


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • artwawA Offline
        artwawA Offline
        artwaw
        wrote on last edited by
        #7

        What if instead of QMap you'd go with QVector of QPair? Seems like more convenient for what you need.

        For more information please re-read.

        Kind Regards,
        Artur

        1 Reply Last reply
        1
        • SPlattenS SPlatten

          @J-Hilk, do I then iterator through keys? I was hoping for something where I could supply a key to look up and it returns the index of the entry? As I said this is easy enough to write, just didn't want to do it if its already available.

          The other part of this problem:
          https://forum.qt.io/topic/131333/ui-finding-control-by-using-literal-string

          Once I have the key I want to use it to construct a control name, but how do I find the control using a string?

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #8

          @SPlatten said in QMap, index of entry without iterating through:

          o I then iterator through keys? I was hoping for something where I could supply a key to look up and it returns the index of the entry? As I said this is easy enough to write, just didn't want to do it if its already available.

          I am not sure to understand what you want to do :(

          A QMap() is an ordered list, so each time you add a new key, index of each key may change. Is QMap() the right type for you need, perhaps QHash() may be better?

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          SPlattenS 1 Reply Last reply
          1
          • KroMignonK KroMignon

            @SPlatten said in QMap, index of entry without iterating through:

            o I then iterator through keys? I was hoping for something where I could supply a key to look up and it returns the index of the entry? As I said this is easy enough to write, just didn't want to do it if its already available.

            I am not sure to understand what you want to do :(

            A QMap() is an ordered list, so each time you add a new key, index of each key may change. Is QMap() the right type for you need, perhaps QHash() may be better?

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by SPlatten
            #9

            @KroMignon , thank you, will use QHasp.

            Kind Regards,
            Sy

            KroMignonK 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @KroMignon , thank you, will use QHasp.

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #10

              @SPlatten said in QMap, index of entry without iterating through:

              are you saying the index order can change with each new insertion into the map?

              Yes I do (cf. https://doc.qt.io/qt-5/containers.html):

              QMap<Key, T> This provides a dictionary (associative array) that maps keys of type Key to values of type T. Normally each key is associated with a single value. QMap stores its data in Key order; if order doesn't matter QHash is a faster alternative.

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              1
              • J.HilkJ J.Hilk

                @SPlatten If I understand you correctly, then keys() is still the function to call.

                it should be ordered by the order of insertion into the QMap, except duplicated keys of course

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

                @J-Hilk said in QMap, index of entry without iterating through:

                @SPlatten If I understand you correctly, then keys() is still the function to call.
                it should be ordered by the order of insertion into the QMap, except duplicated keys of course

                For the record, you should not say this. QMap is not ordered by insertion, it is ordered by keys. And keys() (or values()) comes out in ascending key order. Unless perchance you are talking about the ordering of the values in a multi-map, where within one key the values are ordered by insertion, but I don't think this is what you/OP meant.

                @SPlatten
                Like others, I am confused about what you are storing in the map, and what you are using the values for. And again as others have said, if for some reason you care about an index number, you should not be using a QMap.

                but how do I find the control using a string?

                QObject::findChildren<optionalWidgetType>(optionalWidgetObjectName). Pick whatever is best to start searching down from as the ancestor widget you call findChildren<>() on. (findChild<>() uses this to pick out just one.)

                SPlattenS J.HilkJ 2 Replies Last reply
                1
                • JonBJ JonB

                  @J-Hilk said in QMap, index of entry without iterating through:

                  @SPlatten If I understand you correctly, then keys() is still the function to call.
                  it should be ordered by the order of insertion into the QMap, except duplicated keys of course

                  For the record, you should not say this. QMap is not ordered by insertion, it is ordered by keys. And keys() (or values()) comes out in ascending key order. Unless perchance you are talking about the ordering of the values in a multi-map, where within one key the values are ordered by insertion, but I don't think this is what you/OP meant.

                  @SPlatten
                  Like others, I am confused about what you are storing in the map, and what you are using the values for. And again as others have said, if for some reason you care about an index number, you should not be using a QMap.

                  but how do I find the control using a string?

                  QObject::findChildren<optionalWidgetType>(optionalWidgetObjectName). Pick whatever is best to start searching down from as the ancestor widget you call findChildren<>() on. (findChild<>() uses this to pick out just one.)

                  SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by SPlatten
                  #12

                  @JonB , as suggested by @KroMignon I am now using QHash, I store the host name of remote systems int the map and use the index of there position to select controls that are in a display.

                  I'm now using findChild

                  Kind Regards,
                  Sy

                  JonBJ 1 Reply Last reply
                  0
                  • SPlattenS SPlatten

                    @JonB , as suggested by @KroMignon I am now using QHash, I store the host name of remote systems int the map and use the index of there position to select controls that are in a display.

                    I'm now using findChild

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

                    @SPlatten said in QMap, index of entry without iterating through:

                    and use the index of there position

                    Not sure what you mean by this, though if it's working for you it doesn't matter. I think/assume you are storing some index number as the value against a key in a QHash.

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @J-Hilk said in QMap, index of entry without iterating through:

                      @SPlatten If I understand you correctly, then keys() is still the function to call.
                      it should be ordered by the order of insertion into the QMap, except duplicated keys of course

                      For the record, you should not say this. QMap is not ordered by insertion, it is ordered by keys. And keys() (or values()) comes out in ascending key order. Unless perchance you are talking about the ordering of the values in a multi-map, where within one key the values are ordered by insertion, but I don't think this is what you/OP meant.

                      @SPlatten
                      Like others, I am confused about what you are storing in the map, and what you are using the values for. And again as others have said, if for some reason you care about an index number, you should not be using a QMap.

                      but how do I find the control using a string?

                      QObject::findChildren<optionalWidgetType>(optionalWidgetObjectName). Pick whatever is best to start searching down from as the ancestor widget you call findChildren<>() on. (findChild<>() uses this to pick out just one.)

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by J.Hilk
                      #14

                      @JonB said in QMap, index of entry without iterating through:

                      For the record, you should not say this. QMap is not ordered by insertion, it is ordered by keys.

                      I never said, QMap us ordered by insertion!

                      I said:
                      >keys() is still the function to call. it should be ordered by the order of insertion into the QMap

                      it being here the QList<Keys> that keys() returns.


                      nope, was barking up the wrong tree. @JonB is right


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      JonBJ 1 Reply Last reply
                      1
                      • J.HilkJ J.Hilk

                        @JonB said in QMap, index of entry without iterating through:

                        For the record, you should not say this. QMap is not ordered by insertion, it is ordered by keys.

                        I never said, QMap us ordered by insertion!

                        I said:
                        >keys() is still the function to call. it should be ordered by the order of insertion into the QMap

                        it being here the QList<Keys> that keys() returns.


                        nope, was barking up the wrong tree. @JonB is right

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

                        @J-Hilk
                        As I said, QList<Key> QMap::keys() const (and for that matter values() too) is ordered by ascending key, not by ascending order of key insertion.

                        Returns a list containing all the keys in the map in ascending order.

                        Where do you quote from which says anything about insertion order? QMap does not maintain any insertion order.

                        J.HilkJ 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @J-Hilk
                          As I said, QList<Key> QMap::keys() const (and for that matter values() too) is ordered by ascending key, not by ascending order of key insertion.

                          Returns a list containing all the keys in the map in ascending order.

                          Where do you quote from which says anything about insertion order? QMap does not maintain any insertion order.

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #16

                          @JonB
                          I said, should.

                          And because of the why the template is set up, it iterates over the map

                          1030 template <class Key, class T>
                          1031	Q_OUTOFLINE_TEMPLATE QList<Key> QMap<Key, T>::keys() const
                          1032	{
                          1033	    QList<Key> res;
                          1034	    res.reserve(size());
                          1035	    const_iterator i = begin();
                          1036	    while (i != end()) {
                          1037	        res.append(i.key());
                          1038	        ++i;
                          1039	    }
                          1040	    return res;
                          1041	}
                          

                          so as long as you don't have multiple values to one key,

                          except duplicated keys of course

                          then QMap should be a very linear tree and keys should return in our expected order.

                          Everything with caveats of course, this all breaks very easily :D


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          JonBJ 1 Reply Last reply
                          0
                          • J.HilkJ J.Hilk

                            @JonB
                            I said, should.

                            And because of the why the template is set up, it iterates over the map

                            1030 template <class Key, class T>
                            1031	Q_OUTOFLINE_TEMPLATE QList<Key> QMap<Key, T>::keys() const
                            1032	{
                            1033	    QList<Key> res;
                            1034	    res.reserve(size());
                            1035	    const_iterator i = begin();
                            1036	    while (i != end()) {
                            1037	        res.append(i.key());
                            1038	        ++i;
                            1039	    }
                            1040	    return res;
                            1041	}
                            

                            so as long as you don't have multiple values to one key,

                            except duplicated keys of course

                            then QMap should be a very linear tree and keys should return in our expected order.

                            Everything with caveats of course, this all breaks very easily :D

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

                            @J-Hilk
                            I really do not understand what you are saying. That const_iterator i = begin(); is iterating the QMap in ascending key order, and so that is what keys() returns, not insertion order. I have taken this to a PM post to you to clarify, rather than hijacking this thread.

                            1 Reply Last reply
                            0
                            • J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #18

                              Disregard almost everything I said 🙈,
                              of course the internal storage is sorted on key/value pair insert


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              1 Reply Last reply
                              1

                              • Login

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