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. Problem with QHash container (can't get a key in some condition)
Forum Updated to NodeBB v4.3 + New Features

Problem with QHash container (can't get a key in some condition)

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 1.2k Views
  • 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.
  • K Offline
    K Offline
    Krrris88
    wrote on last edited by
    #5

    It crashes in void MainWindow::addGooseberryToCart(int valueFromSpinBoxAddProduct). Exactly in line:

    qDebug() << (it.key()).getName();
    

    or this one:

    //    ui->labelTest3->setText((it.key()).getName());
    

    In debugger I follow variables. Everything is fine with key until program reach one of these lines mention above. When it happened in key are some trash.

    Sorry, but I'm not sure what do you mean about backtrace (I've never used it). Do you want what is show here:
    How to view Backtrace / Stacktrace?

    KroMignonK 1 Reply Last reply
    0
    • K Krrris88

      It crashes in void MainWindow::addGooseberryToCart(int valueFromSpinBoxAddProduct). Exactly in line:

      qDebug() << (it.key()).getName();
      

      or this one:

      //    ui->labelTest3->setText((it.key()).getName());
      

      In debugger I follow variables. Everything is fine with key until program reach one of these lines mention above. When it happened in key are some trash.

      Sorry, but I'm not sure what do you mean about backtrace (I've never used it). Do you want what is show here:
      How to view Backtrace / Stacktrace?

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

      @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 how it is set?

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

      K 1 Reply Last reply
      1
      • KroMignonK KroMignon

        @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 how it is set?

        K Offline
        K Offline
        Krrris88
        wrote on last edited by
        #7

        @KroMignon

        QHash<Product, int>::iterator it = (shopCart.getCartQHashContainer()).find(productsInShop.at(0));
        
        KroMignonK JonBJ 2 Replies Last reply
        0
        • K Krrris88

          @KroMignon

          QHash<Product, int>::iterator it = (shopCart.getCartQHashContainer()).find(productsInShop.at(0));
          
          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #8

          @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;
          }
          

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

          K 1 Reply Last reply
          3
          • K Krrris88

            @KroMignon

            QHash<Product, int>::iterator it = (shopCart.getCartQHashContainer()).find(productsInShop.at(0));
            
            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #9

            @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?

            1 Reply Last reply
            2
            • KroMignonK KroMignon

              @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;
              }
              
              K Offline
              K Offline
              Krrris88
              wrote on last edited by
              #10

              @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());
              

              ??

              KroMignonK 1 Reply Last reply
              0
              • K Krrris88

                @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());
                

                ??

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

                @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

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

                K 1 Reply Last reply
                5
                • KroMignonK KroMignon

                  @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

                  K Offline
                  K Offline
                  Krrris88
                  wrote on last edited by
                  #12

                  @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.

                  KroMignonK 2 Replies Last reply
                  0
                  • K Krrris88

                    @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.

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

                    @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 the find() 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!");
                    

                    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
                    3
                    • K Krrris88

                      @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.

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

                      @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 again getCartQHashContainer() to get end(), 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?

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

                      K 1 Reply Last reply
                      3
                      • KroMignonK KroMignon

                        @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 again getCartQHashContainer() to get end(), 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?

                        K Offline
                        K Offline
                        Krrris88
                        wrote on last edited by
                        #15

                        @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.

                        KroMignonK 1 Reply Last reply
                        0
                        • K Krrris88

                          @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.

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

                          @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.

                          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
                          4
                          • K Offline
                            K Offline
                            Krrris88
                            wrote on last edited by
                            #17

                            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.

                            KroMignonK 1 Reply Last reply
                            0
                            • K Krrris88

                              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.

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

                              @Krrris88 your welcome and don't forget the mark this topic as solved

                              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
                              0

                              • Login

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