Qt Forum

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

    Call for Presentations - Qt World Summit

    Unable to access elements in a nested QMap

    General and Desktop
    2
    2
    1953
    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.
    • W
      wolf. last edited by

      I am unable to do the simplest of access to a nested QMap

      I have no problem compiling it:
      @QMap<QString, QMap<int, int> > nestedMap;@

      But I can't even do simple checks on it:
      @if(!nestedMap.contains(someKey)) { // <-- segfault at runtime
      // Start adding stuff
      }@

      I partially got around this problem by first loading up the inner QMap and then inserting it into the nested QMap with its respective key.
      @for(i=0; i<rows; i++) {

      QString curkey  = data[rows][1];             // keys are sorted
      int     subkey  = data[rows][2].toInt(&ok);
      int     subval  = data[rows][3].toInt(&ok);
      
      if(oldkey.compare(curkey)) {                 // Changed Keys?
          nestedMap.insert(oldkey, regularMap);    // assemble nestedMap
          dp.clear();
          oldkey = curkey;
      }
      
      if(!dp.contains(subkey)){
          dp[subkey] = subval;
      }
      

      }@

      (The example above needed to be somewhat contrived to the end of isolating the relavant section of code. The logic is effectively the same.)

      But then I'm back to not being able to use the data.
      Testing fails:
      @if(!nestedKey.contains(possibleKey)) // segfault at runtime@

      Extraction fails:
      @regularQMap = nestedMap.value(knownKey); // segfault at runtime@

      Hopefully I'm just failing to see some basic principle of this container, but if anyone has any ideas I'd love to hear them.

      Thank you

      1 Reply Last reply Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by

        There is nothing wrong with nesting QMap.
        It would be my guess there must be something else wrong with your code just manifesting this way.
        Maybe you've got a bad memory write (out of bound etc.) that just happens to hit something in that map and messes it up.
        Maybe it's a bad build. Have you tried clearing and rebuilding your project?
        Maybe you're trying to access a map via an invalid pointer etc.

        Hard to say but the declaration looks ok.

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