PDA

View Full Version : QTSettings with ini file format ignores first section unless preceded by comment



feraudyh
19th March 2014, 18:29
Hi all,
I am getting bizarre behaviour from QSettings
I want to use it with an ini file.
so I do something like this

m_settings = new QSettings("settings.ini",QSettings::IniFormat);
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?

anda_skoa
20th March 2014, 09:14
Works for me



#include <QDebug>
#include <QSettings>
#include <QStringList>
#include <QTemporaryFile>
#include <QTextStream>

int main()
{
QTemporaryFile tmpFile;
if ( !tmpFile.open() ) {
return 1;
}

const QString fileName = tmpFile.fileName();

QTextStream stream( &tmpFile );
stream << QLatin1String( "[Env]" ) << endl << "Driver=QSQLITE" << endl;
stream << "Name=some name" << endl;
tmpFile.close();

QSettings settings( fileName, QSettings::IniFormat );
qDebug() << "childGroups=" << settings.childGroups();

qDebug() << "Driver=" << settings.value("Env/Driver");
qDebug() << "Name=" << settings.value("Env/Name");

return 0;
}

I get


childGroups= ("Env")
Driver= QVariant(QString, "QSQLITE")
Name= QVariant(QString, "some name")


Cheers,
_