Results 1 to 10 of 10

Thread: File path issue...

  1. #1
    Join Date
    Jun 2008
    Posts
    89
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default File path issue...

    Hi,

    I am really confused ..

    The problem is of relative fiile path... Please see attachmed image
    I want to read info.txt file in res folder. i have specifies the relative path as

    Qt Code:
    1. QFile file("res/info.txt")
    To copy to clipboard, switch view to plain text mode 

    it works fine if the exe is created at test.cpp level but if i create the exe at some other path
    the relative path to info.txt is disturbed. Now the file cannot be read....
    I need to make path relative to test.cpp not to the applcation exe.

    Please help.....

    GK
    Attached Images Attached Images

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: File path issue...

    maybe this will help
    Qt Code:
    1. QFile file("../res/info.txt")
    To copy to clipboard, switch view to plain text mode 
    ???

  3. #3
    Join Date
    Jun 2008
    Posts
    89
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: File path issue...

    but it tries to search the file relative to application executable so...every time if the executable is placed at a different path. the relative path will change.

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: File path issue...

    If you run the program from an IDE, working directory will be the one of the project.
    You can do one thing,,, set the working directory of the application. In that way you can always be sure of the relative paths

    Hope i am making sense

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: File path issue...

    I probably don't understand what you mean.
    so, do you have the next structure of your application directory
    application<dir>
    |-----res<dir>
    | |---info.txt<file>
    |-----application.exe<file>
    right?

  6. #6
    Join Date
    Jun 2008
    Posts
    89
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: File path issue...

    application<dir>
    |-----res<dir>
    | |---info.txt<file>
    |build<dir> -- application.exe<file>

    or

    application<dir>
    |-----res<dir>
    | |---info.txt<file>
    |build<dir>test<dir> -- application.exe<file>

    i mean to say that different users of my code will have different paths of storing the executable as per their personal preference .. so how can i be sure that my path to access info.txt does not change with it...
    because the path is relative to application executable not to the file in wihch it is used.

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: File path issue...

    hm, very intresting.
    why an aplication can't be located in one place?
    and for different users you can create needed directory. so, if the application will always be in one place then problem for determination a needed directory never appear.

  8. #8
    Join Date
    Jun 2008
    Posts
    89
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: File path issue...

    But how can govern the others users visual studio and Xcode settings...
    Can Qt Resource System help in this case..?
    If i compile the resourse in to a binary and then use the file path... like we do for images and other stuff?

    Ideas ...?

  9. #9
    Join Date
    Oct 2008
    Location
    Budapest, Hungary
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: File path issue...

    Based on the QT documentation, QFile can read from embedded resource, so you can build the info.txt into the exe and it can be opened and read from the application. Probably you cannot write it.
    Nevertheless if you know the path of your application directory, you can create the absolute filepath of info.txt (QFileInfo) that can be used instead of its relative path.

  10. #10
    Join Date
    Sep 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: File path issue...

    Relative path is relative to current working directory not to the directory in which the executable is stored. If you wish to locate file in path based on executable's directory, you must use something like

    Qt Code:
    1. QFile file(QCoreApplication::applicationDirPath()+"/relative/path");
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Re: Opening Project file Issue in Edyuk
    By philwinder in forum Qt-based Software
    Replies: 6
    Last Post: 5th May 2008, 20:49
  2. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  3. qt3 to qt4 - uic issue
    By hvengel in forum Qt Programming
    Replies: 10
    Last Post: 4th March 2007, 02:59
  4. Replies: 5
    Last Post: 22nd September 2006, 08:04
  5. Qt 3.3.4 -> Qt 4.1.2, moc file issue
    By philski in forum Qt Tools
    Replies: 1
    Last Post: 11th September 2006, 21:08

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.