Solved QHash operator+ is deprecated
-
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
-
Call
iterator.next()
10 times, or calliterator.findNext()
if you can search for the next item. -
@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. -
@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.
-
Keys in QHash are already stored semi-randomly. So there's probably no need to use a random number when accessing at all.
-
@Cocojambo
The originaloperator+=
does exactly the same thing (call++
10 time when you write+= 10
), you just cannot see that...
It's likeint 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.
-
-
@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.