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
Forum Update on Monday, May 27th 2025

problem with QHash

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 709 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 15 Jul 2019, 06:41 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
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 15 Jul 2019, 06:46 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 15 Jul 2019, 07:40
      5
      • S sierdzio
        15 Jul 2019, 06:46

        @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 15 Jul 2019, 07:40 last edited by
        #3

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

        C 1 Reply Last reply 15 Jul 2019, 07:44
        0
        • C cosmoff
          15 Jul 2019, 07:40

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

          C Offline
          C Offline
          cosmoff
          wrote on 15 Jul 2019, 07:44 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 1 Reply Last reply 15 Jul 2019, 07:47
          0
          • C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 15 Jul 2019, 07:46 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
              15 Jul 2019, 07:44

              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 Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 15 Jul 2019, 07:47 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 15 Jul 2019, 08:27
              5
              • J J.Hilk
                15 Jul 2019, 07:47

                @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 15 Jul 2019, 08:27 last edited by
                #7

                it works!
                thank a lot

                C 1 Reply Last reply 15 Jul 2019, 08:33
                1
                • C cosmoff
                  15 Jul 2019, 08:27

                  it works!
                  thank a lot

                  C Online
                  C Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 15 Jul 2019, 08:33 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

                  1/8

                  15 Jul 2019, 06:41

                  • Login

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