Results 1 to 4 of 4

Thread: QSettings - reading from ini files with sections/groups

  1. #1
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QSettings - reading from ini files with sections/groups

    Hello,

    i am new to Qt and trying to build a small application right now which should save its configuration to an .ini file which is checked again on app-launch to do the initial gui filling of my app.

    Basically my ini has the following structure

    [item01]
    id=foo
    path=bar

    [item02]
    id=bla
    path=blub

    Right now i am wondering what might be the best way to read the entire file at the beginning of my application.

    My idea is to count the amount of groups / sections in the ini first.
    In the next step i could fill my UI (QListWidget) element with an QListWidget item for each group of my ini file.

    While i know that i.e.
    //
    QSettings settings( "myininame.ini", QSettings::IniFormat );
    // keys contains all elements of settings
    QStringList keys = settings.allKeys();

    works and delivers me all relevant data - its not offering any sorting-option i would like.
    Reading and writing single elements from/to the .ini works too.
    So i am unsure how to realize the loop-structure to handle each section/group on itself.
    It might help to i.e. count all sections/group at the beginning - but still not sure if i am thinking wrong here.


    I hope my main starting problem is clear- would be great if someone could push my into the right direction - right now i just dont know the amount of sections/groups which makes it difficult for me to know when to end the loop


    Best regards
    el
    Last edited by el; 18th November 2009 at 10:22.

  2. #2
    Join Date
    Jul 2009
    Location
    Italy, Pieve Ligure (GE)
    Posts
    55
    Thanks
    7
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSettings - reading from ini files with sections/groups

    Have you tried QSettings::childKeys() ?

    If used without using any beginGroup(), it returns a string list with all the top level keys ( ["item01", "item02"] in your example). From the list, you can iterate and get the sub-keys of each top-level key.

    childKeys() returns all top-level keys, while childGroups() only returns top-level keys which actually have sub-keys.

    For your example (and in general for INI-like files) this makes no difference, but for registry-based settings, it might.

    M.

  3. #3
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSettings - reading from ini files with sections/groups

    Hi miwarre

    first of all thanks for the reply abnd pointing me into the direction.

    Right now i am wondering why my first attempt does not output anything - but i guess i am still having small issues in my code.


    ini:
    Qt Code:
    1. [0]
    2. id=foo
    3. path=bar
    4. [1]
    5. id=bar
    6. path=bla
    To copy to clipboard, switch view to plain text mode 

    Code:
    Qt Code:
    1. QSettings settings ("myinifile.ini", QSettings::IniFormat);
    2.  
    3. qDebug() << "test";
    4. QStringList childKeys = settings.childKeys();
    5. foreach (const QString &childKey, childKeys)
    6. qDebug() << settings.value(childKey);
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. QSettings settings ("myinifile.ini", QSettings::IniFormat);
    2. QStringList keys = settings.childKeys();
    3. qDebug() << keys;
    To copy to clipboard, switch view to plain text mode 

    seems to end in an empty list too.



    but all in all i guess childKeys was a good hint - so thanks again
    Best regards
    el
    Last edited by el; 20th November 2009 at 09:22.

  4. #4
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSettings - reading from ini files with sections/groups

    solved it - thanks again for the helping hand

Similar Threads

  1. Replies: 3
    Last Post: 26th July 2011, 17:59
  2. Access violation when reading big text files
    By robertson1 in forum General Programming
    Replies: 0
    Last Post: 18th September 2008, 06:59
  3. Reading files in C++
    By maverick_pol in forum General Programming
    Replies: 2
    Last Post: 15th May 2008, 08:53
  4. problem with reading text files
    By Axsis in forum Newbie
    Replies: 1
    Last Post: 25th April 2008, 12:29
  5. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53

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.