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. QMap or QHash doesn't return proper value
Forum Updated to NodeBB v4.3 + New Features

QMap or QHash doesn't return proper value

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 821 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.
  • V Offline
    V Offline
    vijaychsk
    wrote on last edited by vijaychsk
    #1

    Hi,
    In my MainWindow.cc, I have this

    struct Item {
         Item(QString path, QString type) : _path(path), _type(type) {}
         QString _path, _type;
    }
    
    QMap<QString, Item> _myDB;
    

    I'm also populating _myDB in the same file using some method called push.

    push(_search, MainWindow::Item(localPath, localType));
    

    This push method internally calls insert of QMap.

    In the ModelTable.cc, I'm trying to access the _myDB that was already populated. I just want to search and display the results in this file.

    QVariant ModelTable::data(const QModelIndex& index, int role) const {
         Item o = _myDB.value(search); // here seems to be an issue.
         
        switch (o._type) { 
                 case "EE" : return o._path; break;
                 case "FE" : return o._path; break;
                 default: break;
       }
    }
    

    However, I always get an empty string when I do QMap.value(key).

    What am I doing wrong? Why does QMap doesn't return as expected? It is guaranteed that _myDB has some values and my search will always have a result. During the push operation, I can see what is being inserted. But when accessing the data(...), the results aren't as expected. I also tried using QMap<QString, QPair<QString, QString> instead of a struct, still the same issue.
    QMap searching is somehow not working as expected.

    eyllanescE jsulmJ 2 Replies Last reply
    0
    • V vijaychsk

      @jsulm
      _myDB.contains(search) is returning me empty. That is the problem.
      But the search string exists. It is for sure.

      This line is not working: Item o = _myDB.value(search);

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #7

      @vijaychsk what is the output of qDebug() << search << _myDB.keys();?

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      1
      • V vijaychsk

        Hi,
        In my MainWindow.cc, I have this

        struct Item {
             Item(QString path, QString type) : _path(path), _type(type) {}
             QString _path, _type;
        }
        
        QMap<QString, Item> _myDB;
        

        I'm also populating _myDB in the same file using some method called push.

        push(_search, MainWindow::Item(localPath, localType));
        

        This push method internally calls insert of QMap.

        In the ModelTable.cc, I'm trying to access the _myDB that was already populated. I just want to search and display the results in this file.

        QVariant ModelTable::data(const QModelIndex& index, int role) const {
             Item o = _myDB.value(search); // here seems to be an issue.
             
            switch (o._type) { 
                     case "EE" : return o._path; break;
                     case "FE" : return o._path; break;
                     default: break;
           }
        }
        

        However, I always get an empty string when I do QMap.value(key).

        What am I doing wrong? Why does QMap doesn't return as expected? It is guaranteed that _myDB has some values and my search will always have a result. During the push operation, I can see what is being inserted. But when accessing the data(...), the results aren't as expected. I also tried using QMap<QString, QPair<QString, QString> instead of a struct, still the same issue.
        QMap searching is somehow not working as expected.

        eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on last edited by
        #2

        @vijaychsk I did not reproduce the error:

        #include <QMap>
        #include <QDebug>
        
        struct Item {
            Item(const QString & path="", const QString & type="") : _path(path), _type(type) {}
            QString _path, _type;
        };
        
        QDebug operator<<(QDebug debug, const Item &item)
        {
            QDebugStateSaver saver(debug);
            debug.nospace() << '(' << item._path << ", " << item._type << ')';
            return debug;
        }
        
        int main()
        {
        
            QMap<QString, Item> _myDB;
            _myDB["Foo"] = Item("foopath", "footype");
            _myDB["Bar"] = Item("barpath", "bartype");
            _myDB["Baz"] = Item("bazpath", "baztype");
        
            qDebug() << _myDB.value("Foo");
            qDebug() <<  _myDB.value("Bar");
            qDebug() <<  _myDB.value("Baz");
        }
        

        Output:

        ("foopath", "footype")
        ("barpath", "bartype")
        ("bazpath", "baztype")
        

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        V 1 Reply Last reply
        0
        • eyllanescE eyllanesc

          @vijaychsk I did not reproduce the error:

          #include <QMap>
          #include <QDebug>
          
          struct Item {
              Item(const QString & path="", const QString & type="") : _path(path), _type(type) {}
              QString _path, _type;
          };
          
          QDebug operator<<(QDebug debug, const Item &item)
          {
              QDebugStateSaver saver(debug);
              debug.nospace() << '(' << item._path << ", " << item._type << ')';
              return debug;
          }
          
          int main()
          {
          
              QMap<QString, Item> _myDB;
              _myDB["Foo"] = Item("foopath", "footype");
              _myDB["Bar"] = Item("barpath", "bartype");
              _myDB["Baz"] = Item("bazpath", "baztype");
          
              qDebug() << _myDB.value("Foo");
              qDebug() <<  _myDB.value("Bar");
              qDebug() <<  _myDB.value("Baz");
          }
          

          Output:

          ("foopath", "footype")
          ("barpath", "bartype")
          ("bazpath", "baztype")
          
          V Offline
          V Offline
          vijaychsk
          wrote on last edited by
          #3

          @eyllanesc
          On search of QMap, we are getting a value of type Item.
          Can you try assign the contents of the Item and try to print them?

          QString output;
          output = _myDB.value("foo")._path;
          std::cout << output.toStdString() << std::endl;
          

          In my code, I'm not using qdebug but doing assignment. Perhaps there might be some issue there.

          1 Reply Last reply
          0
          • V vijaychsk

            Hi,
            In my MainWindow.cc, I have this

            struct Item {
                 Item(QString path, QString type) : _path(path), _type(type) {}
                 QString _path, _type;
            }
            
            QMap<QString, Item> _myDB;
            

            I'm also populating _myDB in the same file using some method called push.

            push(_search, MainWindow::Item(localPath, localType));
            

            This push method internally calls insert of QMap.

            In the ModelTable.cc, I'm trying to access the _myDB that was already populated. I just want to search and display the results in this file.

            QVariant ModelTable::data(const QModelIndex& index, int role) const {
                 Item o = _myDB.value(search); // here seems to be an issue.
                 
                switch (o._type) { 
                         case "EE" : return o._path; break;
                         case "FE" : return o._path; break;
                         default: break;
               }
            }
            

            However, I always get an empty string when I do QMap.value(key).

            What am I doing wrong? Why does QMap doesn't return as expected? It is guaranteed that _myDB has some values and my search will always have a result. During the push operation, I can see what is being inserted. But when accessing the data(...), the results aren't as expected. I also tried using QMap<QString, QPair<QString, QString> instead of a struct, still the same issue.
            QMap searching is somehow not working as expected.

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

            @vijaychsk said in QMap or QHash doesn't return proper value:

            Item o = _myDB.value(search);

            Are you sure there is such key in your _myDB?
            What does _myDB.contains(search) return?
            value() returns a default constructed value if there is no such key.

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

            V 1 Reply Last reply
            0
            • jsulmJ jsulm

              @vijaychsk said in QMap or QHash doesn't return proper value:

              Item o = _myDB.value(search);

              Are you sure there is such key in your _myDB?
              What does _myDB.contains(search) return?
              value() returns a default constructed value if there is no such key.

              V Offline
              V Offline
              vijaychsk
              wrote on last edited by
              #5

              @jsulm
              _myDB.contains(search) is returning me empty. That is the problem.
              But the search string exists. It is for sure.

              This line is not working: Item o = _myDB.value(search);

              eyllanescE jsulmJ 2 Replies Last reply
              0
              • C Offline
                C Offline
                ChrisW67
                wrote on last edited by
                #6

                @vijaychsk said in QMap or QHash doesn't return proper value:

                Item o = _myDB.value(search); // here seems to be an issue.

                Where does "search" come from? It is not a parameter to the function, and it is not the same as "_search" that you used when you "push" an item onto the map. Is its value what you expect?

                QMap does not have a push() function. Are you sure that what you implemented as push() is doing what you think?

                1 Reply Last reply
                0
                • V vijaychsk

                  @jsulm
                  _myDB.contains(search) is returning me empty. That is the problem.
                  But the search string exists. It is for sure.

                  This line is not working: Item o = _myDB.value(search);

                  eyllanescE Offline
                  eyllanescE Offline
                  eyllanesc
                  wrote on last edited by
                  #7

                  @vijaychsk what is the output of qDebug() << search << _myDB.keys();?

                  If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                  1 Reply Last reply
                  1
                  • V vijaychsk

                    @jsulm
                    _myDB.contains(search) is returning me empty. That is the problem.
                    But the search string exists. It is for sure.

                    This line is not working: Item o = _myDB.value(search);

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

                    @vijaychsk said in QMap or QHash doesn't return proper value:

                    _myDB.contains(search) is returning me empty.

                    What do you mean by "empty"? contains() returns a boolean. So, if it returns false then the map does not contain this key.
                    Print out all keys returned by https://doc.qt.io/qt-5/qmap.html#keys.
                    Do you by any chance have more than one QMap<QString, Item> in your code?

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

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      vijaychsk
                      wrote on last edited by
                      #9

                      Hi everyone,
                      Thank you for the responses. It was a naive mistake on my side.
                      I wrongly populated the _myDB.
                      When I did _myDB.keys() and _myDB.contains(), I got nothing. After I correctly entered the data, the results are as expected.

                      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