QSettings not create the configuration file

Discuss anything related to product development
Post Reply
mortaromarcello
Posts: 7
Joined: Thu Jul 02 2015 7:09 am

QSettings not create the configuration file

Post by mortaromarcello »

Compiling the following code:

Code: Select all

int main(int argc, char *argv[])
{
	QCoreApplication::setOrganizationName("pippo");
	QCoreApplication::setApplicationName("simple");
	QSettings settings;
	qDebug() << settings.fileName();
	settings.setValue("pippo/nome", QString("pluto"));
the program gives me this error:
QFSFileEngine :: open () No file name specified
But settings.fileName () correctly returns the name of the file:
"/home/marcello/.config/pippo/simple.conf"

mortaromarcello
Posts: 7
Joined: Thu Jul 02 2015 7:09 am

Re: QSettings not create the configuration file

Post by mortaromarcello »

It seems that the problem is in the creation of the configuration file. If I create an empty file with the same name and path (touch /home/marcello/.config/pippo/simple.conf) the error disappears, and the values are read and written correctly.

Code: Select all

QString value = settings.value("pippo/nome", QString("paperino!")).toString();
    qDebug() << value;
without the configuration file:
"/home/marcello/.config/pippo/simple.conf"
"paperino!"
QFSFileEngine::open() No file name specified
But the empty directory "/home/marcello/.config/pippo is create.
With the configuration file (first esecution):
"/home/marcello/.config/pippo/simple.conf"
"paperino!"
The configuration file is writed correctly:
[pippo]
nome=pluto
second esecution:
"/home/marcello/.config/pippo/simple.conf"
"pluto"
The configuration file is read correctly.

mortaromarcello
Posts: 7
Joined: Thu Jul 02 2015 7:09 am

Re: QSettings not create the configuration file

Post by mortaromarcello »

When QConfFileSettingsPrivate::syncConfFile (int confFileNo) (in qtsettings.cpp) with instructions

Code: Select all

if (!file.isOpen())
       file.open (QFile::ReadOnly);

tries to open the file in read-only and this does not exist, QFSFileEnginePrivate :: nativeOpen (QIODevice :: OpenMode openMode) (in qfsfileengine_unix.cpp) returned an error (errno = 2 file or directory that does not exist) because the file has not yet been created.
My solution is to change the code:

Code: Select all

if ((!readOnly && confFile->isWritable()))
       file.open(QFile::ReadWrite);
in:

Code: Select all

if ((!readOnly && confFile->isWritable ()) || createFile)
       file.open(QFile::ReadWrite);
being:

Code: Select all

bool createFile = !file.Exists();

Post Reply