Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cross-platform way to change working program directory...
Forum Updated to NodeBB v4.3 + New Features

Cross-platform way to change working program directory...

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 843 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tmason101
    wrote on last edited by
    #1

    Hello,

    I have the following code which changes the working directory of the program to where the program is located. I do this so that my application can correctly find all necessary resources needed to run.

    My question is, what is the cross-platform way to do this in Qt?

    Thank you for your time.

    This is in the int main() function:

    @
    /*

    Change current directory of program to where executable is.
    
    */
    
    std::string ProgramPath(argv[0]);
    std::string WorkingDirectory;
    
    size_t found;
    found = ProgramPath.find_last_of("/\\");
    WorkingDirectory = ProgramPath.substr(0, found);
    

    #if defined(_WIN32)

    _chdir(WorkingDirectory.c_str());
    

    #endif

    #if defined(gnu_linux)

    chdir(WorkingDirectory.c_str());
    

    #endif

    /*
    
    End change of working directory.
    
    */
    

    @

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @
      QDir::​setCurrent(QCoreApplication::​applicationDirPath());
      @

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tmason101
        wrote on last edited by
        #3

        This works! Thank you for your time.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved