PDA

View Full Version : [SOLVED] Issue Using external files with QSettings



Arkahik
13th April 2016, 10:26
Hello everyone,

I'm quite stuck on a little problem with a QSettings. The thing is I have 3 .ini files which contains informations and I would like to parse and use them in my program without compiling them in the .exe or adding them to a QtRessource file.

Those three files should be placed at the root of the compiled project folder, and should be editable.

The following code extract works perfectly for a file in a QtRessource


QSettings my_settings(":/datas/min.ini", QSettings::IniFormat);
ret = my_settings.value("1501x/data01","0").toInt();

But when I try referring to a file outside the QRC, the program fails to load the file apparently, as "ret" takes no value


QSettings my_settings("../min.ini", QSettings::IniFormat);
ret = my_settings.value("1501x/data01","0").toInt();

Can anyone think of something about this ?
Shoud I use a separate C++ parser instead of a QSettings ?

Thanks for your help !

Added after 21 minutes:

Well, seems like I found a suitable solution for this.

The first problem seemed to be how I wrote the path to this file, now I use a QDir::CurrentPath() to get an absolute path for the file, which is quite more suitable.
Second problem was the shadow building feature, I turned it off and it works perfectly.


QSettings my_settings(QDir::currentPath()+"/min.ini", QSettings::IniFormat);
ret = my_settings.value("1501x/data01","0").toInt();

Sorry for the quick-auto-solving post !

anda_skoa
13th April 2016, 10:31
You know that current path will depend on how the application is started, right?

If you want your config files to be at a deterministic location, see QStandardPaths.

Cheers,
_

Arkahik
13th April 2016, 11:05
My files will always be in the same directory than the executable, I don't really get why I should use anything else than my QDir to be honest, giving that I want my path to be dynamically determinated by my .exe.
Maybe I am wrong and don't understand something !

anda_skoa
13th April 2016, 13:37
Well, you said you wanted to write to the files.

In most cases the path of the executable is not writable, unless your program is never installed and only copied by a user to their own data folders.

Cheers,
_

Arkahik
13th April 2016, 13:56
Well those files are initialization data files, they probably might remain unchanged, but I was asked to allow access to those files so they would be edited, with a separate text-editor, but not via the program itself.

I don't get why the path of an installed program would not be accessible, those files cannot be moved elsewhere, instead I load them into a wizard and put them in user's Documents or anything, but considering who will use this program I bet those files will be erased at some point.

anda_skoa
13th April 2016, 14:17
Well those files are initialization data files, they probably might remain unchanged, but I was asked to allow access to those files so they would be edited, with a separate text-editor, but not via the program itself.

Ah, I see, something the person installing the software would do and therefore will have write access to the directory.

But then you will want to use the application path, not the current working directory.



I don't get why the path of an installed program would not be accessible

Because that is usually a program installation location, protected by file system access rules, not a location normal users have write access to.

Cheers,
_

d_stranz
13th April 2016, 16:37
Because that is usually a program installation location, protected by file system access rules, not a location normal users have write access to.

We have a license .ini that we put at install time into the program installation directory. This license file must be updated by the program the first time it is run, when the user registers the software and enters the registration code. Each time the program runs it checks this file for a valid license.

Depending on the user's rights, which can be determined by Windows, the user's system administrator, or the whims of the gods, this write will usually succeed. When it doesn't we have to jump through multiple flaming hoops in order to find a place where we are allowed to both create a new license file (to be used instead of the one we installed) and to write to it. Sometimes we're allowed to create and write in ProgramData since we want this license to apply for all users on the PC; if we aren't then we next try Users, which usually succeeds, but then only the current user gets to run the software because the license check will fail for other users.

So don't make the blanket assumption that you will always be able to create and write ini files in the install directory. Usually you won't be allowed to create files, most often you can read them, and only sometimes will you be allowed to change them.