Faster Way To Convert QObjectList to QMap



  • When reading an INI file using QSettings, I have a settings.children() that is a QObjectList containing both the keys and values inside. Is there a fast way to convert this to a QMap without having to iterate this QObjectList? That way, I can do something like...

    QString breadcrumbColor = iniColors["breadcrumb_links"];

    ...because iniColors would be a QMap of key/value pairs.



  • Hi,

    I'm not sure to understand what do you need.
    Can you post an example of your INI file?



  • Here's the INI file:

    [Colors]
    breadcrumb_links=#CC0000
    breadcrumb_text=#000000
    


  • Hi,

    if you know the keys you can simply do

    settings.beginGroup('Colors');
    QString breadcrumbColor = settings.value("breadcrumb_links").toString();
    QString breadcrumbText = settings.value("breadcrumb_text").toString();
    
    ...
    settings.endGroup();
    

    or, using a map

    QMap<QString, QString> colorMap;
    settings.beginGroup('Colors');
    for (const QString& k: settings.childKeys()) {
        colorMap.insert(k, settings.value(k).toString());
    }
    settings.endGroup();
    


  • @mcosta said:

    QMap<QString, QString> colorMap;
    settings.beginGroup('Colors');
    for (const QString& k: settings.childKeys()) {
        colorMap.insert(k, settings.value(k).toString());
    }
    settings.endGroup();
    

    So, there's no faster way to build the map than with an iterator. Okay, I just wanted to confirm that there wasn't already a preferred routine that would do this for me in like a single line of code, converting from settings.children() into a QMap.


Log in to reply
 

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