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
QtWS25 Last Chance

problem with QHash

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

    Hello everybody,

    I have a problem when I compile this code :

    QHash<QString, double> hash ( it.parameters() );
    
    for(const auto &h : hash)
    {
    	if(h.key() == "value1")
    	{
    		qDebug() << h.value() ;
    	}
    
    }
    

    the compilator return : erreur : request for member ‘key’ in ‘h’, which is of non-class type ‘const double’
    and erreur : request for member ‘value’ in ‘h’, which is of non-class type ‘const double’
    However the key() return the first element so a QString. So what is the problem and how to fix it

    thanks for your help

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

      @cosmoff said in problem with QHash:

      qDebug() << h.value() ;

      "h" is your value, a double. It is not the hash.

      So write this instead:

      qDebug() << hash.value(h) ;
      

      This will print the value for given key (h).

      Also, make hash const or use qAsConst() otherwise it will detach in ranged for loop == worse performance.

      (Z(:^

      C 1 Reply Last reply
      5
      • sierdzioS sierdzio

        @cosmoff said in problem with QHash:

        qDebug() << h.value() ;

        "h" is your value, a double. It is not the hash.

        So write this instead:

        qDebug() << hash.value(h) ;
        

        This will print the value for given key (h).

        Also, make hash const or use qAsConst() otherwise it will detach in ranged for loop == worse performance.

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

        @sierdzio thanks for your answer, and how I can read the key ?

        C 1 Reply Last reply
        0
        • C cosmoff

          @sierdzio thanks for your answer, and how I can read the key ?

          C Offline
          C Offline
          cosmoff
          wrote on last edited by cosmoff
          #4

          In fact, I want to read all the elements key and value of my loop, like :

          for(const auto &h : hash)
              {
                  qDebug() << h.hey() << h.value();
              }
          
          J.HilkJ 1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Then you need to use iterators - Qt's containers return the value in a range-based for loop:

            for (auto it = hash.cbegin();  it != hash.cend(); ++it)
              qDebug() << it.key() << it.value();
            

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            4
            • C cosmoff

              In fact, I want to read all the elements key and value of my loop, like :

              for(const auto &h : hash)
                  {
                      qDebug() << h.hey() << h.value();
                  }
              
              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @cosmoff
              directly taken from the docs:

              QHashIterator<QString, int> i(hash);
              while (i.hasNext()) {
                  i.next();
                  cout << i.key() << ": " << i.value() << endl;
              }
              
              //std style
              QHash<QString, int>::const_iterator i = hash.constBegin();
              while (i != hash.constEnd()) {
                  cout << i.key() << ": " << i.value() << endl;
                  ++i;
              }
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              C 1 Reply Last reply
              5
              • J.HilkJ J.Hilk

                @cosmoff
                directly taken from the docs:

                QHashIterator<QString, int> i(hash);
                while (i.hasNext()) {
                    i.next();
                    cout << i.key() << ": " << i.value() << endl;
                }
                
                //std style
                QHash<QString, int>::const_iterator i = hash.constBegin();
                while (i != hash.constEnd()) {
                    cout << i.key() << ": " << i.value() << endl;
                    ++i;
                }
                
                C Offline
                C Offline
                cosmoff
                wrote on last edited by
                #7

                it works!
                thank a lot

                Christian EhrlicherC 1 Reply Last reply
                1
                • C cosmoff

                  it works!
                  thank a lot

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @cosmoff Then please mark this topic as solved, thx.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  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