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. Insert data into QHash inside QHash

Insert data into QHash inside QHash

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.1k Views 1 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.
  • T Offline
    T Offline
    t0msk
    wrote on last edited by t0msk
    #1

    Hello this is how I declared variable of QHash:

    QHash<QString, QHash<QString, QString>> data
    

    As you can see I want to have QHash inside QHash, because I would like to have one variable which holds some sections like args, settings etc.. and some keys with values so I tried to insert some data into that variable like:

    data.insert("args", "test", "123");
    data.insert("args", "test2", "12345");
    data.insert("settings", "key", "123456");
    

    But I got:

    error: no matching function for call to 'QHash<QString, QHash<QString, QString> >
    

    I understand that my insert is wrong because I insert 3x QString but I have to insert 1x QString and 1x QHash which contains that others 2x QStrings, but I don't know how can I do it, I don't know syntax. So I would be happy if someone helps me.

    Student who loves C/C++

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      One way would be

        using MyHash = QHash<QString, QString> ; // to raise readability 
        QHash<QString, MyHash> data;
        MyHash h1;
        h1.insert("key", "value");
        h1.insert("key2", "value3");  
        data.insert("args", h1);
      

      There might be a fancier syntax but i like this for simplicity :)

      If you want a predefined list, you can also do

      QHash<QString, QHash<QString, QString>> LookupList {
          {"A", {{ "....", "..."}}},
          {"b", {{ "...", "..."}}},
          {"x", {{ "...", "..."}}},
        };
      

      You can of course still insert items at runtime.

      T 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi
        One way would be

          using MyHash = QHash<QString, QString> ; // to raise readability 
          QHash<QString, MyHash> data;
          MyHash h1;
          h1.insert("key", "value");
          h1.insert("key2", "value3");  
          data.insert("args", h1);
        

        There might be a fancier syntax but i like this for simplicity :)

        If you want a predefined list, you can also do

        QHash<QString, QHash<QString, QString>> LookupList {
            {"A", {{ "....", "..."}}},
            {"b", {{ "...", "..."}}},
            {"x", {{ "...", "..."}}},
          };
        

        You can of course still insert items at runtime.

        T Offline
        T Offline
        t0msk
        wrote on last edited by
        #3

        @mrjj If I use predefined list then how can I insert items at runtime? Like in first method:

        MyHash h1;
        h1.insert("key", "value");
        h1.insert("key2", "value3");   
        data.insert("args", h1);
        

        ?

        Student who loves C/C++

        aha_1980A mrjjM 2 Replies Last reply
        0
        • T t0msk

          @mrjj If I use predefined list then how can I insert items at runtime? Like in first method:

          MyHash h1;
          h1.insert("key", "value");
          h1.insert("key2", "value3");   
          data.insert("args", h1);
          

          ?

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @t0msk sure, but why don't you just try it? ;)

          Qt has to stay free or it will die.

          1 Reply Last reply
          2
          • T t0msk

            @mrjj If I use predefined list then how can I insert items at runtime? Like in first method:

            MyHash h1;
            h1.insert("key", "value");
            h1.insert("key2", "value3");   
            data.insert("args", h1);
            

            ?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @t0msk
            yes. same way.
            its 100% the same as first, except there will be already values in it on run.

            T 1 Reply Last reply
            1
            • mrjjM mrjj

              @t0msk
              yes. same way.
              its 100% the same as first, except there will be already values in it on run.

              T Offline
              T Offline
              t0msk
              wrote on last edited by t0msk
              #6

              @mrjj Thank you :)

              @aha_1980 Yea.. :) but there could be another way :D

              Student who loves C/C++

              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