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. Faster Way To Convert QObjectList to QMap
Qt 6.11 is out! See what's new in the release blog

Faster Way To Convert QObjectList to QMap

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.0k Views 2 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.
  • M Offline
    M Offline
    maximo
    wrote on last edited by
    #1

    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.

    M 1 Reply Last reply
    0
    • M maximo

      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.

      M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

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

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maximo
        wrote on last edited by
        #3

        Here's the INI file:

        [Colors]
        breadcrumb_links=#CC0000
        breadcrumb_text=#000000
        
        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          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();
          

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          M 1 Reply Last reply
          0
          • M mcosta

            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();
            
            M Offline
            M Offline
            maximo
            wrote on last edited by
            #5

            @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.

            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