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. Warning "allocating an unneeded temporary container" when getting the index of a QMap key
Forum Updated to NodeBB v4.3 + New Features

Warning "allocating an unneeded temporary container" when getting the index of a QMap key

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.0k 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.
  • D Offline
    D Offline
    Donald Duck
    wrote on last edited by
    #1

    I'm trying to get the index of a key in a QMap. I have this code:

    map.keys().indexOf(someKey);
    

    This works fine, but I get a warning saying "allocating an unneeded temporary container", which suggests that there is a better way to do this. The warning doesn't say what I should do instead though. If searched for how to get the index of a QMap key, but I haven't found anything interesting.

    How do I fix this warning?

    1 Reply Last reply
    0
    • D Donald Duck

      @Christian-Ehrlicher The docs don't seem to have anything to get the index. The method you linked to gives the key of a given value. I already have the key, and I need the index associated with it as a number.

      If you need to know why I need this, it's because I have a QComboBox which is supposed to show values corresponding to the keys:

      QMap<QString, NotImportant> map;
      QComboBox comboBox;
      foreach(const QString &key, map.keys()){
          comboBox.addItem(key);
      }
      comboBox.setCurrentIndex(map.keys().indexOf(someKey));    //This is the line that gives the warning
      

      Since the setCurrentIndex method takes an int, I need the index associated with the key, not the key itself. I could always copy map.keys() into a QList, but that seems unnecessary.

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

      @Donald-Duck
      Since your keys are QString you could save yourself some time/hassle with comboBox.setCurrentText(someKey) :)

      1 Reply Last reply
      3
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by Christian Ehrlicher
        #2

        @Donald-Duck said in Warning "allocating an unneeded temporary container" when getting the index of a QMap key:

        How do I fix this warning?

        By reading the docs. An an index of a map is nothing which you should work with.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        D 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          @Donald-Duck said in Warning "allocating an unneeded temporary container" when getting the index of a QMap key:

          How do I fix this warning?

          By reading the docs. An an index of a map is nothing which you should work with.

          D Offline
          D Offline
          Donald Duck
          wrote on last edited by
          #3

          @Christian-Ehrlicher The docs don't seem to have anything to get the index. The method you linked to gives the key of a given value. I already have the key, and I need the index associated with it as a number.

          If you need to know why I need this, it's because I have a QComboBox which is supposed to show values corresponding to the keys:

          QMap<QString, NotImportant> map;
          QComboBox comboBox;
          foreach(const QString &key, map.keys()){
              comboBox.addItem(key);
          }
          comboBox.setCurrentIndex(map.keys().indexOf(someKey));    //This is the line that gives the warning
          

          Since the setCurrentIndex method takes an int, I need the index associated with the key, not the key itself. I could always copy map.keys() into a QList, but that seems unnecessary.

          JonBJ 1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Again: you should and must not use an index of a map since this is nothing a map can be described with.
            Since you already create a container you can use this:

            const auto keys = map.keys();
            comboBox.addItems(keys);
            comboBox.setCurrentIndex(keys.indexOf(someKey));
            

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1
            • D Donald Duck

              @Christian-Ehrlicher The docs don't seem to have anything to get the index. The method you linked to gives the key of a given value. I already have the key, and I need the index associated with it as a number.

              If you need to know why I need this, it's because I have a QComboBox which is supposed to show values corresponding to the keys:

              QMap<QString, NotImportant> map;
              QComboBox comboBox;
              foreach(const QString &key, map.keys()){
                  comboBox.addItem(key);
              }
              comboBox.setCurrentIndex(map.keys().indexOf(someKey));    //This is the line that gives the warning
              

              Since the setCurrentIndex method takes an int, I need the index associated with the key, not the key itself. I could always copy map.keys() into a QList, but that seems unnecessary.

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

              @Donald-Duck
              Since your keys are QString you could save yourself some time/hassle with comboBox.setCurrentText(someKey) :)

              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 Extensions
              • Unsolved