Results 1 to 11 of 11

Thread: currentPath is started always where the executable is?

  1. #1
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default currentPath is started always where the executable is?

    Hi, I need to know the directory where the application is stored because I have to save some configuration files in the directoy "config" placed on this directory. My question is: "QDir::currentPath" always "points" at the beginning of the app at this directory in all platforms? I think so, but I want to know it for sure Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine, L'viv
    Posts
    57
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: currentPath is started always where the executable is?

    Let's look what Assistant says:
    QString QDir::currentPath () [static]
    Returns the absolute path of the application's current directory.
    It seems it does

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: currentPath is started always where the executable is?

    An application can be launched from a different directory where the executable itself lies.
    "Current dir" is assumably the directory where the executable was launched from.
    So I wouldn't suggest using that kind of approach..

    Maybe you could just use QSettings instead? Or alternatively QDir::homePath()..
    (Note that the latter might be problematic on windows, though.)
    J-P Nurmi

  4. The following user says thank you to jpn for this useful post:

    Dark_Tower (7th April 2006)

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: currentPath is started always where the executable is?

    Let the example speak for itself:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDir>
    3. #include <QtDebug>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8. qDebug() << QDir::currentPath();
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 

    C:\Documents and Settings\JP\My Documents\Visual Studio Projects\CurrentDir\Debug>CurrentDir.exe
    "C:/Documents and Settings/JP/My Documents/Visual Studio Projects/CurrentDir/Debug"

    C:\Documents and Settings\JP\My Documents\Visual Studio Projects\CurrentDir\Debug>cd ..

    C:\Documents and Settings\JP\My Documents\Visual Studio Projects\CurrentDir>Debug\CurrentDir.exe
    "C:/Documents and Settings/JP/My Documents/Visual Studio Projects/CurrentDir"

    C:\Documents and Settings\JP\My Documents\Visual Studio Projects\CurrentDir>cd ..

    C:\Documents and Settings\JP\My Documents\Visual Studio Projects>CurrentDir\Debug\CurrentDir.exe
    "C:/Documents and Settings/JP/My Documents/Visual Studio Projects"
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    Dark_Tower (7th April 2006)

  7. #5
    Join Date
    Jan 2006
    Location
    Ukraine, L'viv
    Posts
    57
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: currentPath is started always where the executable is?

    Quote Originally Posted by jpn
    An application can be launched from a different directory where the executable itself lies.
    "Current dir" is assumably the directory where the executable was launched from.
    So I wouldn't suggest using that kind of approach..

    Maybe you could just use QSettings instead? Or alternatively QDir::homePath()..
    (Note that the latter might be problematic on windows, though.)
    Oops... You are right. I didn't think about this case.

  8. #6
    Join Date
    Feb 2006
    Location
    Boulder, Colorado, USA
    Posts
    63
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: currentPath is started always where the executable is?

    Try this:

    QString QCoreApplication::applicationDirPath () [static]
    Returns the directory that contains the application executable.
    For example, if you have installed Qt in the C:\Trolltech\Qt directory, and you run the regexp example, this function will return "C:/Trolltech/Qt/examples/tools/regexp".
    On Mac OS X this will point to the directory actually containing the executable, which may be inside of an application bundle (if the application is bundled).
    Warning: On Unix, this function assumes that argv[0] contains the file name of the executable (which it normally does). It also assumes that the current directory hasn't been changed by the application.
    From Qt docs

  9. The following user says thank you to jrideout for this useful post:

    Dark_Tower (7th April 2006)

  10. #7
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: currentPath is started always where the executable is?

    Quote Originally Posted by L.Marvell
    Let's look what Assistant says:

    It seems it does
    I also have looked at the docs... The problem is the one that jpn has mentioned.
    Last edited by Dark_Tower; 7th April 2006 at 16:24.

  11. #8
    Join Date
    Jan 2006
    Location
    Ukraine, L'viv
    Posts
    57
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: currentPath is started always where the executable is?

    Quote Originally Posted by Dark_Tower
    I also have looked at the docs... The problem is the one that jpn has mentioned.
    Yep, I've already seen. It's quite interesting but jpn's code does nothing to me. I never wrote console apps with Qt, maybe I do something wrong. I've just copied code and did standard qmake -project, qmake, make. Result of running app:
    C:\temp\test\release>test.exe


    C:\temp\test>
    P.S. Forgot to say. Using Qt 4.1.2 with MinGW
    Last edited by L.Marvell; 7th April 2006 at 16:44.

  12. #9
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: currentPath is started always where the executable is?

    L.Marvell you have to start jpn's code on debug mode or in other case show the current path with a message box

  13. #10
    Join Date
    Jan 2006
    Location
    Ukraine, L'viv
    Posts
    57
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: currentPath is started always where the executable is?

    Quote Originally Posted by Dark_Tower
    L.Marvell you have to start jpn's code on debug mode or in other case show the current path with a message box
    I wish to help you, but you helped me Thanks!

  14. #11
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: currentPath is started always where the executable is?

    Quote Originally Posted by L.Marvell
    I wish to help you, but you helped me Thanks!
    I'm thankful for your good intentions, too

Similar Threads

  1. Qt3 qprocess, executable exited?
    By triperzonak in forum Newbie
    Replies: 2
    Last Post: 22nd September 2008, 11:21

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.