Results 1 to 2 of 2

Thread: QTSettings with ini file format ignores first section unless preceded by comment

  1. #1
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QTSettings with ini file format ignores first section unless preceded by comment

    Hi all,
    I am getting bizarre behaviour from QSettings
    I want to use it with an ini file.
    so I do something like this
    Qt Code:
    1. m_settings = new QSettings("settings.ini",QSettings::IniFormat);
    To copy to clipboard, switch view to plain text mode 
    Now if I have a first "section"( beginning with [Env]) then all settings in that section are ignored: reading with m_settings->value(...) returns the "fallback" value.
    The following sections are properly read however
    Now if I precede by first section with a comment starting with # then mysteriously all settings in that ini file are read properly.
    Is this a bug or a feature or is a Byte Order Mark doing its dirty work here?
    Last edited by feraudyh; 19th March 2014 at 18:32. Reason: make it clearer

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTSettings with ini file format ignores first section unless preceded by comment

    Works for me

    Qt Code:
    1. #include <QDebug>
    2. #include <QSettings>
    3. #include <QStringList>
    4. #include <QTemporaryFile>
    5. #include <QTextStream>
    6.  
    7. int main()
    8. {
    9. QTemporaryFile tmpFile;
    10. if ( !tmpFile.open() ) {
    11. return 1;
    12. }
    13.  
    14. const QString fileName = tmpFile.fileName();
    15.  
    16. QTextStream stream( &tmpFile );
    17. stream << QLatin1String( "[Env]" ) << endl << "Driver=QSQLITE" << endl;
    18. stream << "Name=some name" << endl;
    19. tmpFile.close();
    20.  
    21. QSettings settings( fileName, QSettings::IniFormat );
    22. qDebug() << "childGroups=" << settings.childGroups();
    23.  
    24. qDebug() << "Driver=" << settings.value("Env/Driver");
    25. qDebug() << "Name=" << settings.value("Env/Name");
    26.  
    27. return 0;
    28. }
    To copy to clipboard, switch view to plain text mode 
    I get
    Qt Code:
    1. childGroups= ("Env")
    2. Driver= QVariant(QString, "QSQLITE")
    3. Name= QVariant(QString, "some name")
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. QtSettings and QStringList
    By matsukan in forum Qt Programming
    Replies: 4
    Last Post: 24th February 2014, 20:51
  2. Replies: 2
    Last Post: 10th August 2011, 07:16
  3. Replies: 5
    Last Post: 1st June 2011, 08:28
  4. Qt Creator Comment Section
    By ToddAtWSU in forum Qt Tools
    Replies: 3
    Last Post: 6th February 2011, 21:01
  5. QMessageBox ignores nobr with rich text format
    By daggilli in forum Qt Programming
    Replies: 0
    Last Post: 14th October 2008, 03:54

Tags for this Thread

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.