Results 1 to 6 of 6

Thread: QProcess::setWorkingDirectory not functioning properly

  1. #1
    Join Date
    May 2006
    Posts
    58
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QProcess::setWorkingDirectory not functioning properly

    I am spawning an external program from my QT4 app as follows:
    Qt Code:
    1. //Extract working directory for .exe
    2. QFileInfo fileInfo(m_sLaunchFilename);
    3. QString fileName = fileInfo.fileName();
    4.  
    5. //Manually reconstructing path because fileInfo.absolutePath switches
    6. // direction of slashes, and I'm trying to eliminate possible problems
    7. QString sPath = m_sLaunchFilename.left( m_sLaunchFilename.length() - fileName.length() );
    8.  
    9. //Need just because it's a required argument
    10. QStringList obList;
    11.  
    12. //Create process object to launch external .exe
    13. QProcess obProcess;
    14.  
    15. //Set working directory of .exe to whatever directory it lives in
    16. obProcess.setWorkingDirectory( sPath );
    17. //Retrieve for debugging
    18. sPath = obProcess.workingDirectory();
    19.  
    20. //Run it!
    21. obProcess.startDetached(m_sLaunchFilename, obList);
    22. qDebug() << "Launching " << m_sLaunchFilename << " in working directory " << sPath;
    To copy to clipboard, switch view to plain text mode 

    The output is as follows:

    Launching "/code/workingdir//workingdir" in working directory "/code/workingdir//"

    I have written a little app specifically to help debug this problem. The code of the app is as follows:

    Qt Code:
    1. int main( int argc, char* argv[] ) {
    2. QApplication app( argc, argv );
    3.  
    4.  
    5. qDebug() << "Current path is " << QDir::currentPath();
    6.  
    7. FILE *fp = fopen("shawsync.ini", "r" );
    8. if( !fp ) {
    9. QMessageBox::information(NULL, "Error", "Unable to open shawsync.ini!");
    10. printf("Unable to open shawsync.ini\n");
    11. }
    12. else {
    13. QMessageBox::information(NULL, "Success", "Success: opened shawsync.ini!");
    14. fclose(fp);
    15. }
    16. } //main
    To copy to clipboard, switch view to plain text mode 

    I put a text file "shawsync.ini" in the directory where I built this program. When I manually run this program from the command line, I get the output:

    Current path is "/code/workingdir"

    And a messagebox denoting that shawsync.ini was found. When it is launched from my other app using QProcess, the output is:

    Current path is "/code/hardupdater"

    And a message box denoting that shawsync.ini was not found. I have tested this in both Windows and Linux, and neither one is working. The new process is always started in the directory where I ran the parent process (in this case, /code/hardupdater), and the setWorkingDirectory seems to be ignored.

  2. #2
    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: QProcess::setWorkingDirectory not functioning properly

    Setting the working directory to a instance of QProcess has no effect to QProcess::startDetached() because it is a static method.
    J-P Nurmi

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

    hardgeus (4th December 2006)

  4. #3
    Join Date
    May 2006
    Posts
    58
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess::setWorkingDirectory not functioning properly

    Quote Originally Posted by jpn View Post
    Setting the working directory to a instance of QProcess has no effect to QProcess::startDetached() because it is a static method.
    I didn't notice that it was static because I was calling calling it with an object, but you're right, it is documented as static. Now the question becomes: Is it possible to set the working directory for a detached process? My app is launching the external program on exit so I have to detach it. If I don't detach it, the child process is killed with my app.

  5. #4
    Join Date
    May 2006
    Posts
    58
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess::setWorkingDirectory not functioning properly

    Got it. I'm now manually using QDir to set the current working directory before calling startDetached, and then setting it back:
    Qt Code:
    1. QString sOldPath = QDir::currentPath();
    2. QDir::setCurrent( sPath );
    3.  
    4. //Run it!
    5. obProcess.startDetached(m_sLaunchFilename, obList);
    6. qDebug() << "Launching " << m_sLaunchFilename << " in working directory " << sPath;
    7.  
    8. QDir::setCurrent( sOldPath );
    To copy to clipboard, switch view to plain text mode 

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

    tpf80 (27th June 2007)

  7. #5
    Join Date
    Oct 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProcess::setWorkingDirectory not functioning properly

    Right way

    Qt Code:
    1. QProcess process;
    2. process.setProgram(file);
    3. process.setArgument(args);
    4. process.setWorkingDirectory(dir);
    5. process.startDetached();
    To copy to clipboard, switch view to plain text mode 

  8. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QProcess::setWorkingDirectory not functioning properly

    No, wrong way.

    As was stated earlier, QProcess::startDetached() is a static class method and therefore changes made to a specific QProcess instance (like setting the working directory, etc.) have no effect on the detached process. Static class methods, whether you call them using process.startDetached() or QProcess::startDetached() have no access to non-static member variables of a particular instance.

    In Qt5, there is a version of QProcess::startDetached() that has arguments to provide the working directory in which the process will be started. That is the "right way".
    Last edited by d_stranz; 14th February 2018 at 18:10.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Removing items properly from qtreewidget item
    By Djony in forum Qt Programming
    Replies: 6
    Last Post: 21st November 2006, 13:20
  2. Replies: 1
    Last Post: 12th November 2006, 17:56
  3. Why can't I make dynamic_cast work properly?
    By pir in forum General Programming
    Replies: 13
    Last Post: 18th July 2006, 17:17
  4. How to cleanup timers properly?
    By jpn in forum Qt Programming
    Replies: 7
    Last Post: 6th July 2006, 18:52
  5. Replies: 1
    Last Post: 2nd June 2006, 00:54

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.