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. How to use QHash / MusltiHash - hint needed
Forum Updated to NodeBB v4.3 + New Features

How to use QHash / MusltiHash - hint needed

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 574 Views 2 Watching
  • 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.
  • ademmlerA Offline
    ademmlerA Offline
    ademmler
    wrote on last edited by
    #1

    Hi to all,

    I want to create a QHash(Multi), which has the following content (Pseudo)example:

    "QString Key" ->  int val 1, int val2, int val3
    

    Is this possible and if yes - how?

    thx and regards Alex

    jsulmJ 1 Reply Last reply
    0
    • S Offline
      S Offline
      SimonSchroeder
      wrote on last edited by
      #5

      I am not sure that the OP is actually looking for a multi hash. A multi hash stores any number of entries under the same hash key. In the case that you always want to store exactly 3 integers combine the hash with a tuple:
      QHash<QString,std::tuple<int,int,int>> hash;
      You can then access the second value with std::get<1>(hash["key"]). Notice that in C++ we always start counting from 0. So the second value has index 1.

      I personally consider using a tuple the quick & dirty approach. If your three integers have a specific purpose it is much better to write a little struct:

      struct my_data_t
      {
        int a;
        int b;
        int c;
      };
      
      QHash<QString,my_data_t> hash;
      ...
      hash["key"].b;
      
      1 Reply Last reply
      3
      • ademmlerA ademmler

        Hi to all,

        I want to create a QHash(Multi), which has the following content (Pseudo)example:

        "QString Key" ->  int val 1, int val2, int val3
        

        Is this possible and if yes - how?

        thx and regards Alex

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

        @ademmler said in How to use QHash / MusltiHash - hint needed:

        Is this possible and if yes - how?

        Yes it is. What exactly is the problem?
        In documentation you can even find an example for exactly what you're asking: https://doc.qt.io/qt-5/qmultihash.html

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

        1 Reply Last reply
        3
        • ademmlerA Offline
          ademmlerA Offline
          ademmler
          wrote on last edited by
          #3

          Thx for answering:

          You mean this example:

          QMultiHash<QString, int> hash1, hash2, hash3;
          hash1.insert("plenty", 100);
          hash1.insert("plenty", 2000);
          

          In my example this would be:
          QMultiHash<QString, int> hash;
          hash.insert("plenty", 100);
          hash.insert("plenty", 2000);
          hash.insert("plenty", 50);

          How to access the second value? Is there something like

          hash.getValue(2);
          

          I could not find such a routine. It would always need:

          QList<int> values = hash.values("plenty");   values.at(2);
          
          Christian EhrlicherC 1 Reply Last reply
          0
          • ademmlerA ademmler

            Thx for answering:

            You mean this example:

            QMultiHash<QString, int> hash1, hash2, hash3;
            hash1.insert("plenty", 100);
            hash1.insert("plenty", 2000);
            

            In my example this would be:
            QMultiHash<QString, int> hash;
            hash.insert("plenty", 100);
            hash.insert("plenty", 2000);
            hash.insert("plenty", 50);

            How to access the second value? Is there something like

            hash.getValue(2);
            

            I could not find such a routine. It would always need:

            QList<int> values = hash.values("plenty");   values.at(2);
            
            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by Christian Ehrlicher
            #4

            @ademmler said in How to use QHash / MusltiHash - hint needed:

            I could not find such a routine. It would always need:

            How should it work otherwise? Since you don't know how many elements you get for one value you must not directly access the third (in your case) element but check the size of the returned container first.

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

            ademmlerA 1 Reply Last reply
            1
            • S Offline
              S Offline
              SimonSchroeder
              wrote on last edited by
              #5

              I am not sure that the OP is actually looking for a multi hash. A multi hash stores any number of entries under the same hash key. In the case that you always want to store exactly 3 integers combine the hash with a tuple:
              QHash<QString,std::tuple<int,int,int>> hash;
              You can then access the second value with std::get<1>(hash["key"]). Notice that in C++ we always start counting from 0. So the second value has index 1.

              I personally consider using a tuple the quick & dirty approach. If your three integers have a specific purpose it is much better to write a little struct:

              struct my_data_t
              {
                int a;
                int b;
                int c;
              };
              
              QHash<QString,my_data_t> hash;
              ...
              hash["key"].b;
              
              1 Reply Last reply
              3
              • Christian EhrlicherC Christian Ehrlicher

                @ademmler said in How to use QHash / MusltiHash - hint needed:

                I could not find such a routine. It would always need:

                How should it work otherwise? Since you don't know how many elements you get for one value you must not directly access the third (in your case) element but check the size of the returned container first.

                ademmlerA Offline
                ademmlerA Offline
                ademmler
                wrote on last edited by
                #6

                Hi Christian,

                It could be a fixed set of entries - which might not be modified.
                However @SimonSchroeder gave me a nice idea what to do.

                thx guys.

                Christian EhrlicherC 1 Reply Last reply
                0
                • ademmlerA ademmler

                  Hi Christian,

                  It could be a fixed set of entries - which might not be modified.
                  However @SimonSchroeder gave me a nice idea what to do.

                  thx guys.

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

                  @ademmler said in How to use QHash / MusltiHash - hint needed:

                  It could be a fixed set of entries - which might not be modified.

                  Then a multihash is not the correct container as already pointed out by @SimonSchroeder

                  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
                  0

                  • Login

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