Results 1 to 3 of 3

Thread: Trouble with relative paths for files

  1. #1
    Join Date
    Aug 2009
    Location
    United States
    Posts
    45
    Thanks
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Trouble with relative paths for files

    I have a lot of things like QIcons that I need to specify a path->filename for, but I don't want to do an absolute path because the project's directory will not always be constant.

    I discovered the QCoreApplication::applicationDirPath() function, which gives the path to the executable file, but my images are a directory above. How can I move up a level? I tried stuff like:

    Qt Code:
    1. QString temp = QCoreApplication::applicationDirPath() + "../icon.png";
    2. setWindowIcon( QIcon(temp) );
    To copy to clipboard, switch view to plain text mode 

    But I haven't had any luck so far.

    Is there an easy way to use relative paths for files?
    Last edited by N3wb; 29th December 2010 at 23:30.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Trouble with relative paths for files

    See QDir and QDir::cdUp

    Basically, you can give QCoreApplication::applicationDirPath() to the constructor of QDir, then perform cdUp function on that object and retrieve new directory. If it's not possible to go further up (eg. if the application is in the root directory), the cdUp method will fail.

  3. #3
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Default Re: Trouble with relative paths for files

    Also, you could output the result of your string concatenation (or go in debug mode) to be sure of what you're doing.
    My guess is that you missed a "/" after application dir path

    Qt Code:
    1. QString temp = QCoreApplication::applicationDirPath() + "/" + ".." + "/icon.png";
    2. qDebug() << temp ;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Trouble Connecting to Microsoft Access *.accdb files
    By FuNkDaDdY in forum Qt Programming
    Replies: 1
    Last Post: 10th November 2010, 10:18
  2. Relative paths in Qt
    By drmath in forum Qt Programming
    Replies: 2
    Last Post: 30th June 2010, 16:50
  3. X11 include and lib paths
    By Oluwafemi in forum Installation and Deployment
    Replies: 1
    Last Post: 3rd April 2010, 15:25
  4. Problems with paths
    By zorro68 in forum Installation and Deployment
    Replies: 1
    Last Post: 13th July 2008, 11:20
  5. How to set paths?
    By hoborg in forum Installation and Deployment
    Replies: 11
    Last Post: 24th February 2006, 19: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.