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. QHash in QList in QHash
Forum Updated to NodeBB v4.3 + New Features

QHash in QList in QHash

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 7.0k 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.
  • X Offline
    X Offline
    xiit
    wrote on last edited by
    #1

    Basically I was wondering if it is possible to put a QHash in a QList which I then put in a QHash.

    This is my code:

    @ QHash<QString, QVariant> info;

    info.insert("someinfo", "blah blah");
    info.insert("moreinfo", 123456);
    
    QList<QHash<QString, QVariant> > list;
    
    QHash<QString, QVariant> blah;
    
    blah.insert("some", "stuff");
    
    list << blah;
    
    info.insert("somelist", list);@
    

    But the last line (14) obviously gives me an error as I do not know the proper way to approach this.

    @error: no matching function for call to 'QHash<QString, QVariant>::insert(const char [9], QList<QHash<QString, QVariant> >&)'

    candidates are: QHash<Key, T>::iterator QHash<Key, T>::insert(const Key&, const T&) [with Key = QString, T = QVariant]@

    Any ideas?

    Thanks in advance.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Try this:
      @
      info.insert("somelist", QVariant::fromValue(list));
      @

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sam
        wrote on last edited by
        #3

        [quote author="Krzysztof Kawa" date="1358076526"]Try this:
        @
        info.insert("somelist", QVariant::fromValue(list));
        @[/quote]

        Tried but didnt work. I think we need to register using Q_DECLARE_METATYPE for the list but that too didn't work.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Just tried this and it prints "bar" as expected:
          @
          #include <QDebug>
          #include <QList>
          #include <QHash>
          #include <QVariant>
          #include <QString>

          int main()
          {
          QHash<QString, QVariant> hash;
          hash["foo"] = "bar";

          QList<QHash<QString, QVariant> > list;
          list << hash;
          
          QHash<QString, QVariant> hash2;
          hash2.insert("buzz", QVariant::fromValue(list));
          
          
          
          QList<QHash<QString, QVariant> > bar;
          bar = hash2["buzz"].value<QList<QHash<QString, QVariant> > >();
          
          qDebug() << bar[0]["foo"].toString();
          
          return 0;
          

          }
          @

          1 Reply Last reply
          0
          • U Offline
            U Offline
            utcenter
            wrote on last edited by
            #5

            Or... be a man and use typedefs :P

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              [quote author="utcenter" date="1358092235"]Or... be a man and use typedefs :P [/quote]

              Well sure, in a production code. But the point here was to show it actually works with those types, thus the overly verbose syntax.

              1 Reply Last reply
              0
              • U Offline
                U Offline
                utcenter
                wrote on last edited by
                #7

                ^^I didn't mean you but the OP, your example is perfectly adequate.

                Even if for some reason it is necessary to put a hash in a list in a hash, doing so with the raw containers themselves is not something I would consider a good practice, it is much better to build up this kind of structure with typedefs, wrapper classes and accessor methods to make the whole process with working with this transparent.

                1 Reply Last reply
                0
                • X Offline
                  X Offline
                  xiit
                  wrote on last edited by
                  #8

                  Okay, now I've managed to store it with the following code:

                  @ QVariantHash info;

                  info.insert("someinfo", "blah blah");
                  info.insert("moreinfo", 123456);
                  
                  //
                  
                  QVariantList list;
                  
                  QVariantHash blah;
                  blah.insert("some", "stuff");
                  
                  list << blah;
                  
                  info.insert("somelist", list);
                  
                  //
                  
                  QVariantHash myHash = info["somelist"].toList().at(0).toHash();
                  
                  qDebug() << myHash["some"].toString(); // outputs "stuff"@
                  

                  Is there any "nicer" way of doing this?

                  EDIT: I'm new to both C++ and Qt so I pretty much have no idea what I am doing, just playing around. :P

                  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