Solved Problem with QHash container (can't get a key in some condition)
-
@Krrris88 said in Problem with QHash container (can't get a key in some condition):
qDebug() << (it.key()).getName();
Are you sure that it has a valid state?
Can you show howit
is set? -
QHash<Product, int>::iterator it = (shopCart.getCartQHashContainer()).find(productsInShop.at(0));
-
@Krrris88 And did you check if it is valid before call qDebug(), something like this:
auto hash = shopCart.getCartQHashContainer(); auto it = hash.find(productsInShop.at(0)); while (it != hash.end()) { qDebug() << (it.key()).getName(); ++it; }
-
@Krrris88
As @KroMignon has said.
https://doc.qt.io/qt-5/qhash.html#find says:If the hash contains no item with the key, the function returns
end()
.In which case, if
it == end()
, how does your(it.key()).getName()
behave? -
@KroMignon I tried your code and it works. Also I implement it to my code:
auto it = (shopCart.getCartQHashContainer()).find(productsInShop.at(0)); if(it != (shopCart.getCartQHashContainer()).end()) ui->labelTest3->setText((it.key()).getName());
And it also works :)
But know it's question: WHY? Sorry but I have no idea how it works. Why one if-statement change it? There is still the SAME iterator.
Why something like this can't work:auto it = (shopCart.getCartQHashContainer()).find(productsInShop.at(0)); ui->labelTest3->setText((it.key()).getName());
??
-
@Krrris88 said in Problem with QHash container (can't get a key in some condition):
Why something like this can't work:
Because, when calling
find()
from QHash, you are not sure it will give you non empty result (iterator is at the end of the container, which is an invalid/null item).
So you have to check if the result is valid.I would suggest you to take time to read documentation of QHash and QHash iterator
-
@KroMignon I know it. I know that find() return end(), if in container is no object with proper key and that is invalid.
But tell me, if I have iterator:
auto it = (shopCart.getCartQHashContainer()).find(productsInShop.at(0));
Does this if-statement change the iterator or what? Because in both case the way of set the iterator is the same. In both situations iterator point to the same thing (proper thing, it's not end()), so why without if-statement it doesn't work? Hope you understand my point.
-
@Krrris88 said in Problem with QHash container (can't get a key in some condition):
Does this if-statement change the iterator or what?
No, this
if
statement do not change the iterator state, I think there are situations for which thefind()
does return an empty iterator. You can find it out with this code:auto hash=shopCart.getCartQHashContainer(); auto it = hash.find(productsInShop.at(0)); if(it != hash.end()) ui->labelTest3->setText((it.key()).getName()); else ui->labelTest3->setText("EMPTY ITERATOR!");
-
@Krrris88 said in Problem with QHash container (can't get a key in some condition):
Does this if-statement change the iterator or what?
I reply again, because there is something other I have not take care about!
When you call
getCartQHashContainer()
, it will, I think returns you a copy of the QHash, depending on signature of this fonction!
So the find will be done of a copy of the QMap.
Then you call againgetCartQHashContainer()
to getend()
, which will be another item..For me,
getCartQHashContainer()
should return a reference of the QMap:QHash<Product, int> & getCartQHashContainer();
or with const, if you don't want to change map outside the class
QHash<Product, int> & getCartQHashContainer() const;
Is this the case?
-
@KroMignon I do not think that it's the problem with copy/reference. Look that I take twice container, but it's the same copy. Nothing change. First getter take copy for iterator and second for end(). But between these two lines nothing change container. So I think it only takes more memory and time for copy instead works on original container. Nevertheless I'll try it. Also I'll check your earlier idea (if/else) and let you know about results.
-
@Krrris88 said in Problem with QHash container (can't get a key in some condition):
I do not think that it's the problem with copy/reference.
But I do, look at this:
QHash<int, QString> hash1; hash1[0] = "AAA"; hash1[1] = "BB"; hash1[2] = "CCC"; hash1[3] = "DDD"; hash1[4] = "EEE"; QHash<int, QString> hash2(hash1); qDebug() << (hash1.end() == hash2.end() ? "OK" : "FAILURE");
When executing this, you will obtain FAILURE.
Try it out, if you don't believe me. -
Yes, you're right with this end(). I changed getter and now it returns reference. It looks program works. Hope it's enough and there will be no more errors. Thanks for help.
-
@Krrris88 your welcome and don't forget the mark this topic as solved