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. Getting multiple values from one key
Qt 6.11 is out! See what's new in the release blog

Getting multiple values from one key

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 976 Views
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    If I have INI file with this format:

    [TAG1]
    name=value1,value2,value3,...
    
    [TAG2]
    name=value1,value2,value3,...
    
    [TAG3]
    name=value1,value2,value3,...
    

    How to get JSON object that looks like this:

    {"TAG1":["value1","value2",...], "TAG2":["value1","value2",...]}
    

    I've tried to get them like this:

    QSettings settings(":/data.ini",QSettings::IniFormat);
    QStringList groups= settings.childGroups();
    foreach (QString group, groups)
    {
        settings.beginGroup(group);
        QStringList keys = settings.childKeys();
        QJsonArray arr;
        foreach (key, keys) {
            values=settings.value(key);
            QJsonObject object;
            QJsonValue valueJson(values.toString());
            object.insert(key,valueJson);
            arr.append(object);
        }
        qDebug() <<"Array: "<< arr;
        jsonObj.insert(group,arr);
        qDebug()<<jsonObj;
    
        settings.endGroup()
    
        }
    

    But what I get as QJsonObject is:

    QJsonObject({"TAG1":[{"name":""}],"TAG2":[{"name":""}],"TAG3":[{"name":""}]})
    I dont want "name", only values as I wrote up there.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Guest said in Getting multiple values from one key:

      settings.value(key);

      Since the returned QVariant holds a QStringList QVariant::toString() will return an empty string since there is no automatic conversion between a QStringList and a QString

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      5

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved