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. QHash operator+ is deprecated
Forum Updated to NodeBB v4.3 + New Features

QHash operator+ is deprecated

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 697 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.
  • C Offline
    C Offline
    Cocojambo
    wrote on last edited by
    #1

    Hi. Is there any alternative variant instead of that deprecated? It works correctly but just want to get rid of this intrusive message in correct way (without disabling notice messages).

    QHash<QString, QString> list;
    QHash<QString, QString>::iterator iterator = list.begin();
    iterator += 10; // <- Notice: 'operator+' is deprecated  
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Call iterator.next() 10 times, or call iterator.findNext() if you can search for the next item.

      (Z(:^

      C 1 Reply Last reply
      0
      • sierdzioS sierdzio

        Call iterator.next() 10 times, or call iterator.findNext() if you can search for the next item.

        C Offline
        C Offline
        Cocojambo
        wrote on last edited by
        #3

        @sierdzio
        Well ... it's quiet questionable solution to call iterator ten times in empty loop :)
        I use it for getting random item from the list. So "10" can be any value < list.size().
        And findNext() in my case is useless.

        jsulmJ 1 Reply Last reply
        0
        • C Cocojambo

          @sierdzio
          Well ... it's quiet questionable solution to call iterator ten times in empty loop :)
          I use it for getting random item from the list. So "10" can be any value < list.size().
          And findNext() in my case is useless.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Cocojambo Call https://doc.qt.io/qt-5/qhash.html#keys to get a QList with the keys and then use random number to access random key and use it to get the value.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            Keys in QHash are already stored semi-randomly. So there's probably no need to use a random number when accessing at all.

            (Z(:^

            C 1 Reply Last reply
            1
            • B Offline
              B Offline
              Bonnie
              wrote on last edited by
              #6

              @Cocojambo
              The original operator+= does exactly the same thing (call ++ 10 time when you write += 10), you just cannot see that...
              It's like

              int j = 10; //the value you pass to it
              while(j--) ++iterator;
              

              And according to the doc

              This operator is deprecated in order to align with std::unordered_map functionality.

              So I think there won't be any alternative function to replace it.

              1 Reply Last reply
              4
              • sierdzioS sierdzio

                Keys in QHash are already stored semi-randomly. So there's probably no need to use a random number when accessing at all.

                C Offline
                C Offline
                Cocojambo
                wrote on last edited by
                #7

                @sierdzio
                But we will get the same "random" element if we call it several times (if QHash wasn't changed).

                @jsulm was thinking about it but it should be slower. Needs to test)

                @Bonnie Thanks, it makes sense.

                sierdzioS 1 Reply Last reply
                0
                • C Cocojambo

                  @sierdzio
                  But we will get the same "random" element if we call it several times (if QHash wasn't changed).

                  @jsulm was thinking about it but it should be slower. Needs to test)

                  @Bonnie Thanks, it makes sense.

                  sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  @Cocojambo said in QHash operator+ is deprecated:

                  But we will get the same "random" element if we call it several times (if QHash wasn't changed).

                  Simply take the next index each time you need a "random" element. My point is that there is (probably) no need to randomize the index. All depends on how really random you need your result to be.

                  (Z(:^

                  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