Results 1 to 3 of 3

Thread: Best multiplatform policy to store per-user profile files?

  1. #1
    Join Date
    Jul 2008
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Best multiplatform policy to store per-user profile files?

    Hi everybody,

    As this is my first message, I shall precise that I am a Qt newbie, although highly enthousiast about it!

    I am currently coding my first real Qt application, which needs every user to have a personal Sqlite database. The situation is somehow similar to Firefox's cache file, which are properly stored into the user directory, and the problem is the same: figuring out where to store the file depending on the platform the application is running on.

    I already use QSettings for my application settings, but it only allows to store small key/value pairs. Plus, on Windows the default policy is to use the registry, while I need a file system.

    I have been thinking about the following workaround:

    1) Create a QSettings(QSettings::IniFormat)
    2) Call QSettings::fileName() to get the name of the configuration file
    3) Strip the file name to keep the directory it is stored in
    4) Add my own name

    So ok, the generated file name would be what I would like it to be, but isn't there a Qt class that is better suited to handle this kind of cases?

    Thanks,
    Alex.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Best multiplatform policy to store per-user profile files?

    You can use QSettings constructor with your fileName

    Qt Code:
    1. QString mySettings = QApplication::applicationDirPath() + "/mySettings.conf";
    2.  
    3. QSettings settings(mySettings, QSettings::IniFormat);
    4.  
    5. .....
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Jul 2008
    Posts
    24
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Best multiplatform policy to store per-user profile files?

    Wouldn't it return the same path for all users using the same installation? (e.g. /usr/bin/ on Linux)

    I expected to generate something more like /home/user/.myprogram/ in order to have something unique. Eventually I came with this solution to create a per-user profile directory and register it to the settings:

    Qt Code:
    1. void checkUserProfileDirectory()
    2. {
    3. QSettings settings;
    4. QString profileDirName;
    5. if (!settings.contains("userProfile")) {
    6. profileDirName = QDir(QSettings(QSettings::IniFormat, QSettings::UserScope,
    7. QCoreApplication::organizationName(), QCoreApplication::applicationName()).fileName()).path();
    8. profileDirName.resize(profileDirName.lastIndexOf('.'));
    9. settings.setValue("userProfile", profileDirName);
    10. }
    11. else profileDirName = settings.value("userProfile").toString();
    12.  
    13. QDir profileDir(profileDirName);
    14. if (!profileDir.exists()) profileDir.mkpath(".");
    15. }
    To copy to clipboard, switch view to plain text mode 

    I'd gladly take any better solution though.

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.