Results 1 to 4 of 4

Thread: Read Several Groups in INI file from QSettings

  1. #1
    Join Date
    Jan 2011
    Location
    Sri Lanaka
    Posts
    64
    Thanks
    39
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Post Read Several Groups in INI file from QSettings

    i need to read all the groups in my ini file... how can i do this?

    this is my ini file
    Qt Code:
    1. [sys_1]
    2. val1 = 10
    3. val2 = 23
    4. val3 = 23
    5.  
    6. [sys_2]
    7. val1 = 56
    8. val2 = 2
    9. val3 = 27
    10.  
    11. [sys_3]
    12. val1 = 84
    13. val2 = 65
    14. val3 = 12
    To copy to clipboard, switch view to plain text mode 

    this is the code im using

    Qt Code:
    1. QSettings settings(QDir::currentPath() + "/test.ini", QSettings::IniFormat);
    2. settings.beginGroup("SYS_1");// How to change this to read 'n' number of groups ??
    3. const QStringList childKeys = settings.childKeys();
    4.  
    5. QStringList values;
    6.  
    7.  
    8. foreach (const QString &childKey, childKeys)
    9. {
    10. Keys << childKey;
    11. values << settings.value(childKey).toString();
    12. }
    13. settings.endGroup();
    To copy to clipboard, switch view to plain text mode 

    settings.beginGroup("SYS_1");// How to change this to read 'n' number of groups ??

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read Several Groups in INI file from QSettings

    You can read all group using QSettings::childGroups()
    for example

    Qt Code:
    1. QSettings settings(QDir::currentPath() + "/test.ini", QSettings::IniFormat);
    2. Q_FOREACH (QString group, settings.childGroups()) {
    3. settings.beginGroup(group);
    4. const QStringList childKeys = settings.childKeys();
    5.  
    6. QStringList values;
    7.  
    8. foreach (const QString &childKey, childKeys)
    9. {
    10. Keys << childKey;
    11. values << settings.value(childKey).toString();
    12. }
    13. settings.endGroup();
    14. }
    To copy to clipboard, switch view to plain text mode 

    or, if you read ONLY "sys_*" groups

    Qt Code:
    1. QSettings settings(QDir::currentPath() + "/test.ini", QSettings::IniFormat);
    2. Q_FOREACH (QString group, settings.childGroups()) {
    3. if (!group.startsWith("sys_"))
    4. continue;
    5.  
    6. settings.beginGroup(group);
    7. const QStringList childKeys = settings.childKeys();
    8.  
    9. QStringList values;
    10.  
    11. foreach (const QString &childKey, childKeys)
    12. {
    13. Keys << childKey;
    14. values << settings.value(childKey).toString();
    15. }
    16. settings.endGroup();
    17. }
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  3. The following user says thank you to mcosta for this useful post:

    deepal_de (4th July 2011)

  4. #3
    Join Date
    Jan 2011
    Location
    Sri Lanaka
    Posts
    64
    Thanks
    39
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Read Several Groups in INI file from QSettings

    How Can i add a new Group using QSettings??

    my ini is this
    Qt Code:
    1. [sys_1]
    2. val1 = 10
    3. val2 = 23
    4. val3 = 23
    5.  
    6. [sys_2]
    7. val1 = 56
    8. val2 = 2
    9. val3 = 27
    10.  
    11. [sys_3]
    12. val1 = 84
    13. val2 = 65
    14. val3 = 12
    To copy to clipboard, switch view to plain text mode 

    i need to add [sys_4]..

  5. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read Several Groups in INI file from QSettings

    Using setValue

    Qt Code:
    1. QSettings settings (...);
    2.  
    3. settings.addGroup("[sys_4]");
    4. settings.setValue("val1", 10);
    5. settings.setValue("val2", 20);
    6. settings.setValue("val3", 30);
    7. settings.endGroup();
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. How to use QSettings to read INI file
    By Cantora in forum Newbie
    Replies: 8
    Last Post: 16th June 2011, 08:14
  2. Replies: 1
    Last Post: 14th January 2011, 11:57
  3. Replies: 3
    Last Post: 20th November 2009, 14:32
  4. QSettings , read only avaiable?
    By patrik08 in forum Qt Programming
    Replies: 5
    Last Post: 18th November 2007, 15:21
  5. Using QSettings to read pre-made INI file..
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 05:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.