Search code examples
c++qtqmap

Unable to read out config file while iterating


I am having trouble accessing an iterator when trying to read from a config file. My code is:

void requestPLC::write(QMap <QString, QVariant> map)
{

QMap<QString, QVariant>::iterator i;

for (i = map.begin(); i != map.end(); ++i)
    {
        mConfig->reqPLC->datatype.value(map[i]);
        // do something here
    }
}

What I want to do is iterate over my map and pick the corresponding value from the config file. My error looks like this:

no match for ‘operator[]’ (operand types are ‘QMap’ and ‘QMap::iterator’) mConfig->reqPLC->datatype.value(map[i]); ^

I know this is caused by datatype.value(map[i]) being an iterator but I can't figure out a way to avoid this. Do I need to cast my iterator here or something?


Solution

  • Accessing the key, value of the QMap using an iterator can be done via the iterator class

    In your case:

    void requestPLC::write(QMap <QString, QVariant> map)
    {
    
        QMap<QString, QVariant>::iterator i;
    
        for (i = map.begin(); i != map.end(); ++i)
        {
            auto map_value = i.value();
            mConfig->reqPLC->datatype.value(map_value);
            // do something here
        }
    }