+ Reply to Thread
Results 1 to 9 of 9

Thread: How to use QSettings to read INI file

  1. #1
    Join Date
    Mar 2009
    Location
    Malaysia
    Posts
    25
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default How to use QSettings to read INI file

    Hi All,

    I am trying to use QSettings to read the INI file but I have try few times but still fail to read it


    The format for INI file is like below:

    [System.Setting]
    auto_check_update=false
    auto_check_mode=weekly

    Thanks

  2. #2
    Join Date
    Mar 2009
    Location
    Malaysia
    Posts
    25
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use QSettings to read INI file

    Found out the way

    QString strKey("System.Setting/");
    QSettings * settings = 0;
    settings = new QSettings( m_strWorkingPath, QSettings::IniFormat );
    ret = settings->value( strKey + "auto_check_update", "r").toBool();
    strMode = settings->value( strKey + "auto_check_mode", "r").toString();

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,368
    Thanks
    19
    Thanked 992 Times in 903 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to use QSettings to read INI file

    Quote Originally Posted by Cantora View Post
    QString strKey("System.Setting/");
    Better use QSettings::beginGroup() and QSettings::endGroup().

  4. The following user says thank you to Lykurg for this useful post:

    Cantora (5th June 2009)

  5. #4

    Default Re: How to use QSettings to read INI file

    Hi, for me
    The format for INI file is like below:

    [players]
    player0name=mike
    player0surname=jane
    player1name=scootie
    ...
    ...

    I am trying to use QSettings to read the INI file..
    I tried this:
    settings = new QSettings("config.ini",QSettings::IniFormat);
    settings->beginGroup("players");
    int size = settings->beginReadArray("player");
    ui->lineEdit_2->setText(QString::number(size)); // this is value-test

    for (int index = 0; index < size; ++index)
    {
    settings->setArrayIndex(index);
    ui->choosePlayerBox->addItem(settings->value("name").toString());
    }

    settings->endArray();
    settings->endGroup();

    But it doesn`t work..
    I tried also:
    [players]
    player/0/name=mike
    player/0/surname=jane
    player/1/name=scootie
    ...
    ...
    But it also doesn`t work..
    Any ideas ?
    Thanks

  6. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 365 Times in 334 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to use QSettings to read INI file

    try this example:
    Qt Code:
    1. QSettings settings("test.ini", QSettings::IniFormat);
    2. settings.beginGroup("players");
    3. const QStringList childKeys = settings.childKeys();
    4. foreach (const QString &childKey, childKeys)
    5. qDebug() << settings.value(childKey);
    6. settings.endGroup();
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #6

    Default Re: How to use QSettings to read INI file

    ok, i have ini file like:
    [user]
    0/name=lukasz
    0/wins=3
    0/loses=3
    1/name=pawel
    1/wins=1
    1/loses=5
    2/name=anna
    2/wins=1
    2/loses=5

    and my code is:
    Qt Code:
    1. settings = new QSettings("config.ini",QSettings::IniFormat);
    2. settings->beginGroup("user");
    3. QStringList player_number = settings->childGroups(); // returns 0, 1, 2 - OK !
    4. const QStringList childKeys = settings->childKeys(); // should return name, wins, ... right ?
    5. foreach(const QString &childKey, childKeys)
    6. {
    7. ui->choosePlayerBox->addItem(settings->value(childKey).toString()); // should add lukasz, 3, 3, pawel...., but it doesn`t work
    8.  
    9. }
    10. settings->endGroup();
    To copy to clipboard, switch view to plain text mode 

    Any ideas ?

  8. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 365 Times in 334 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to use QSettings to read INI file

    you code will not work, because "0/name" it's a key, not a group.
    I suggest you to review format on your ini-file or use xml instead.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. The following user says thank you to spirit for this useful post:

    candyshop (16th November 2009)

  10. #8
    Join Date
    May 2010
    Location
    Bangalore, India.
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to use QSettings to read INI file

    Hi
    my .ini file is

    [System.Setting]
    auto_check_update=false
    auto_check_mode=weekly

    & the code to read is
    QString strKey("System.Setting/");
    QSettings * settings = 0;
    settings = new QSettings( file_path, QSettings::IniFormat );
    QString strMode = settings->value( strKey + "auto_check_mode", "r").toString();

    no problem with the code compiling & running. when i try to print strMode program is unexpectedly finising.. this is happening with QString, Date (Derived data types). but fine with basic data types like int, float, bool...
    using printf to print the values. qDebug is not working in this project. what could be the problem?

  11. #9
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,689
    Thanks
    3
    Thanked 408 Times in 393 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to use QSettings to read INI file

    Does your derived data type(Date) support QVariant ?
    QSettings use QVariant to work.

+ Reply to Thread

Similar Threads

  1. Replies: 5
    Last Post: 27th May 2009, 12:49
  2. Read binary from file
    By weldpua2008 in forum Newbie
    Replies: 2
    Last Post: 3rd April 2009, 23:50
  3. QFile can't read a file
    By Raccoon29 in forum Qt Programming
    Replies: 3
    Last Post: 11th February 2009, 20:24
  4. [Java] read and write a file
    By mickey in forum General Programming
    Replies: 3
    Last Post: 22nd June 2008, 10:43
  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