Results 1 to 7 of 7

Thread: Shared Files

  1. #1
    Join Date
    Mar 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Shared Files

    Hi all,

    I currently developing a Linux application which uses some external xml files to hold report schemas, etc.

    Under Linux those files are normally stored in /usr/share/<app_name> or /usr/share/local/<app_name>, as the user should not be allowed to change them. I could include them as resources in the binary directly, but prefer to have them outside.

    Now my question, how do I know in the source code where do find the xml filse?

    For QFile I have to specify a filename, but the file could be anywhere on the harddisc. Is there a prefered way to deal with this? I'm surely not the only one trying to use "external" files.

    Many thanks.

    Marco

  2. #2
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Shared Files

    QFile can take a whole path + filename in its argument, and you can also use QDir along with it for more advanced features. You can also do a path like this:

    Qt Code:
    1. //this opens a file in the app's installed folder for example.
    2. filepath = qApp->applicationDirPath() + "/" + filename;
    3.  
    4.  
    5. QFile file(filepath);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Shared Files

    You could place your xml files in "/etc/<app>/" or you create your own application directories (etc, lib, ...) and place it into the "/opt/<app>" directory.

    <app> could be "QApplication::applicationName()", so if you have the following folder structure:
    Qt Code:
    1. /opt
    2. /opt/demo
    3. /opt/demo/bin
    4. /opt/demo/etc
    5. /opt/demo/log
    To copy to clipboard, switch view to plain text mode 

    You could than write:
    Qt Code:
    1. // Get the application config (/opt/demo/etc/demo.xml)
    2. QString configFilename = QString("%0%1..%1etc%1demo.xml").arg(qApp->applicationDirPath()).arg(QDir::separator());
    3. QString logFilename = QString("%0%1..%1log%1demo.log").arg(qApp->applicationDirPath()).arg(QDir::separator());
    4.  
    5. QFile configFile(configFilename);
    6. QFile logFile(logFilename);
    7. .
    8. .
    To copy to clipboard, switch view to plain text mode 

    Regards NoRulez

  4. #4
    Join Date
    Mar 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Shared Files

    Hello,

    Many thanks for your replies. What I'm wondering is how other projects deal with such problems.

    My application works on Mac OS X and Linux. This means that on a Linux system, my report definitions will be in a folder like /usr/share/<appname> or /usr/local/share/<appname>, while on a Mac OS X system the files would be within the application bundle in the Contents folder.

    My questions are:

    1. How do I now where the files are installed on a Linux system? Should I use a config file stored in /etc to hold the exact location of the report definitions or is there a better way?

    2. How do I deal with the cross-platform problem?

    Many thanks for your help.

    Regards,
    Marco

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Shared Files

    I see two possibilities.

    1. You choose any location and hard code the path in your app using Q_OS_UNIX or Q_OS_MAC etc. to set different location for the different platforms. Then you must ensure, that the admin who installs your app place the file in the right location.
    2. You use internal an ordered list with all possible paths and loop through that list till you find a valid file each time your app starts. To optimize you could store the founded path in a user config file.


    Lykurg

    EDIT:
    How do I now where the files are installed on a Linux system
    You are the application developer you decide! You could use qmake:install to define the path for example or use the native *dev or *rpm or ebuild....

  6. #6
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Shared Files

    What I would probably do is use QSettings to store the location where the reports will be.

    Then, I would have code which runs when the program starts up to load the settings using QSettings.

    If no settings are found (meaning first time this program has been used), then make the default settings in QSettings using Q_OS_UNIX, Q_OS_MAC, Q_OS_WIN for the platform that the program is installed on.

    You could then give the user the option of changing where they want the reports to come from in a dialog, which would alter the settings. Or if you have an installer for each OS and give the user the option to choose the install path within it, the installer could make the initial settings which the program would read on startup.
    Last edited by tpf80; 31st May 2009 at 21:29.

  7. #7
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Shared Files

    you can get some common locations (DOcuments, Desktop, Temp ...) by using QDesktopServices::storageLocation(), so maybe there is also some interesting location for you.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. Replies: 12
    Last Post: 17th June 2009, 05:34
  2. visual studio project files - adding extra files
    By luf in forum Qt Programming
    Replies: 3
    Last Post: 13th June 2008, 21:05
  3. compiling problem / Qt DLL files
    By ht1 in forum Newbie
    Replies: 1
    Last Post: 5th January 2008, 17:58
  4. Replies: 5
    Last Post: 22nd September 2006, 08:04
  5. [Win32/VC++ 8.0] Strange problems with qrc_*.cpp files
    By mloskot in forum Installation and Deployment
    Replies: 6
    Last Post: 6th March 2006, 10:28

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.