Results 1 to 7 of 7

Thread: [SOLVED] Issue Using external files with QSettings

  1. #1
    Join Date
    Apr 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default [SOLVED] Issue Using external files with QSettings

    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

    Qt Code:
    1. QSettings my_settings(":/datas/min.ini", QSettings::IniFormat);
    2. ret = my_settings.value("1501x/data01","0").toInt();
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. QSettings my_settings("../min.ini", QSettings::IniFormat);
    2. ret = my_settings.value("1501x/data01","0").toInt();
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. QSettings my_settings(QDir::currentPath()+"/min.ini", QSettings::IniFormat);
    2. ret = my_settings.value("1501x/data01","0").toInt();
    To copy to clipboard, switch view to plain text mode 

    Sorry for the quick-auto-solving post !
    Last edited by Arkahik; 13th April 2016 at 10:27. Reason: Problem solved

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,359
    Thanks
    36
    Thanked 1,533 Times in 1,484 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [SOLVED] Issue Using external files with QSettings

    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,
    _

  3. #3
    Join Date
    Apr 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [SOLVED] Issue Using external files with QSettings

    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 !

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,359
    Thanks
    36
    Thanked 1,533 Times in 1,484 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [SOLVED] Issue Using external files with QSettings

    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,
    _

  5. #5
    Join Date
    Apr 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [SOLVED] Issue Using external files with QSettings

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,359
    Thanks
    36
    Thanked 1,533 Times in 1,484 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [SOLVED] Issue Using external files with QSettings

    Quote Originally Posted by Arkahik View Post
    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.

    Quote Originally Posted by Arkahik View Post
    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,
    _

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,014
    Thanks
    233
    Thanked 636 Times in 626 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: [SOLVED] Issue Using external files with QSettings

    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.

Similar Threads

  1. Issue with Qsettings
    By MM5 in forum Newbie
    Replies: 4
    Last Post: 19th July 2015, 06:11
  2. QSettings - Sync issue between two process
    By kartlee in forum Qt Programming
    Replies: 1
    Last Post: 26th August 2011, 22:19
  3. QSettings load and write problem in files
    By a.m.blub in forum Qt Programming
    Replies: 2
    Last Post: 11th April 2011, 06:10
  4. Replies: 3
    Last Post: 8th December 2009, 03:29
  5. Replies: 3
    Last Post: 20th November 2009, 14:32

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.