Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved problem with QHash

    General and Desktop
    4
    8
    294
    Loading More Posts
    • 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
      cosmoff last edited by cosmoff

      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 Reply Quote 0
      • sierdzio
        sierdzio Moderators last edited by

        @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 Reply Quote 5
        • C
          cosmoff @sierdzio last edited by

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

          C 1 Reply Last reply Reply Quote 0
          • C
            cosmoff @cosmoff last edited by 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.Hilk 1 Reply Last reply Reply Quote 0
            • Christian Ehrlicher
              Christian Ehrlicher Lifetime Qt Champion last edited by

              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 has to stay free or it will die.

              1 Reply Last reply Reply Quote 4
              • J.Hilk
                J.Hilk Moderators @cosmoff last edited by

                @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

                Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

                C 1 Reply Last reply Reply Quote 5
                • C
                  cosmoff @J.Hilk last edited by

                  it works!
                  thank a lot

                  Christian Ehrlicher 1 Reply Last reply Reply Quote 1
                  • Christian Ehrlicher
                    Christian Ehrlicher Lifetime Qt Champion @cosmoff last edited by

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

                    Qt has to stay free or it will die.

                    1 Reply Last reply Reply Quote 1
                    • First post
                      Last post