Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting multiple values from one key
Qt 6.12 LTS 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 1.0k 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 Online
      Christian EhrlicherC Online
      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
      • Unsolved