Results 1 to 5 of 5

Thread: QProcess - Working directory problem

  1. #1
    Join Date
    Sep 2008
    Posts
    26
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QProcess - Working directory problem

    Hello,

    I'm trying to run an external program using the QProcess class. As I got some problems, for the moment, it is just a small sh script displaying one environment variable and the working directory :

    Qt Code:
    1. #!/bin/sh
    2.  
    3. pwd
    4. echo $dir
    To copy to clipboard, switch view to plain text mode 

    Here is the code I use to run this script from my Qt program :

    Qt Code:
    1. void S3geoa2Launcher::runS3geoa2()
    2. {
    3.  
    4. QString program = "/usr2/home/jomarin/apps/testScript";
    5.  
    6. QString dirPath = "dir=";
    7.  
    8. dirPath.append("testUnix");
    9.  
    10. // create or modify the dir environment variable
    11. putenv((char *)dirPath.toStdString().c_str());
    12.  
    13. QProcess *s3geoa2Process = new QProcess(this);
    14.  
    15. // Setting the working directory of the application
    16. s3geoa2Process->setWorkingDirectory(QString(avantDistributeur->getPath().c_str()));
    17.  
    18. cout << "Launching " << program.toStdString() << endl;
    19. cout << "Working directory "<< avantDistributeur->getPath() << endl;
    20. cout << "Real working directory of the process "<< s3geoa2Process->workingDirectory().toStdString() << endl;
    21.  
    22. // Running the test script
    23. s3geoa2Process->startDetached(program);
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

    Finally, here is the output I get on the terminal :

    Qt Code:
    1. Launching /usr2/home/jomarin/apps/testScript
    2. Working directory /usr2/home/jomarin/testUnix/cadre/avd
    3. Real working directory of the process /usr2/home/jomarin/testUnix/cadre/avd
    4. /usr2/home/jomarin/svn_repo/alstomTools/trunk/debug
    5. testUnix
    To copy to clipboard, switch view to plain text mode 

    The dir environement variable is correctly set, but the working directory of the program is wrong .

    Am I doing something wrong ?

    Thanks in advance,

    Joel Marin.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QProcess - Working directory problem

    The dir environement variable is correctly set
    Are you sure?
    from the code, the dir environment variable is:
    "dir=testUnix"

    yet all you other paths are not in '/' (root).
    All the rest of the output matches what ever you gave in your code...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2008
    Posts
    26
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess - Working directory problem

    Are you sure?
    from the code, the dir environment variable is:
    "dir=testUnix"
    In fact, the test script shows that the dir environment variable is set to "testUnix". That is what I expected.

    yet all you other paths are not in '/' (root).
    All the rest of the output matches what ever you gave in your code...
    What do you mean ? For me, I specified the working directory of the process as :
    /usr2/home/jomarin/testUnix/cadre/avd
    and when running, the pwd command gives :
    /usr2/home/jomarin/svn_repo/alstomTools/trunk/debug

    Joel.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QProcess - Working directory problem

    I don't understand:
    the output shows that the real working directory is the same one as you are setting.
    Which variable is not as you expect?
    You mean the pwd in your script?
    That is because the pwd in your scripts puts out where it is in.
    Your exe is either in /project_path/debug or /project_path/release depending on your configuration, so you need to set your working directory appropriately.

    Btw - you don't need to convert from c_str() to QString() - just use getPath() like that:
    Qt Code:
    1. s3geoa2Process->setWorkingDirectory(avantDistributeur->getPath());
    To copy to clipboard, switch view to plain text mode 

    It should not really change things though.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Sep 2008
    Posts
    26
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess - Working directory problem

    Thanks for your answer, I resolved my problem using the QDir::setCurrent() function to set the working directory and it works.

    Anyway, I still don't understand the behaviour of the QProcess::setWorkingDirectory() method.

    From the documentation, I read :

    void QProcess::setWorkingDirectory ( const QString & dir )

    Sets the working directory to dir. QProcess will start the process in this directory. The default behavior is to start the process in the working directory of the calling process.
    Thus, I don't understand why the pwd command of my script returns
    /usr2/home/jomarin/svn_repo/alstomTools/trunk/debug
    which is the working directory of my exe and not
    /usr2/home/jomarin/testUnix/cadre/avd
    which is the directory from where I want the new process to be started.

    Could you clarify the use of the QProcess::setWorkingDirectory() method ?

    Joel.

Similar Threads

  1. setting working directory for current process
    By mule in forum Qt Programming
    Replies: 1
    Last Post: 8th October 2007, 13:54
  2. Problem with Parent QWidget and Buttons not working
    By VireX in forum Qt Programming
    Replies: 7
    Last Post: 11th May 2007, 22:24
  3. QProcess not working in some cases
    By munna in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2006, 09:58
  4. QProcess / system call not working under linux. Why?
    By johnny_sparx in forum Qt Programming
    Replies: 12
    Last Post: 11th March 2006, 00:32
  5. QT Service and working directory
    By Lele in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2006, 11:46

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.