Unable to access elements in a nested QMap



  • 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


  • Moderators

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.